PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In web development, scripting can occur on the client side (in the user’s browser) or the server side (on the web server). PHP is primarily used as a server-side scripting language, but it’s important to understand both concepts and how PHP fits into the broader web development landscape.

Server-Side Scripting with PHP

Server-side scripting involves scripts that run on the web server before the content is sent to the user’s web browser. PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages. Here’s how it works:

  1. Request: A user requests a web page by typing a URL or clicking a link. This request is sent to the web server.
  2. Processing: The web server processes the request using PHP scripts. These scripts can perform various tasks like accessing a database, processing form data, or generating dynamic page content.
  3. Response: After processing, the server sends the generated HTML, CSS, and possibly JavaScript back to the user’s browser for display.

Advantages of Server-Side Scripting with PHP:

  • Security: Since PHP code is executed on the server, users cannot see the source code. This helps protect sensitive information like database credentials.
  • Interactivity: PHP can generate dynamic content based on user interactions, such as logging in or submitting a form.
  • Database Interaction: PHP can interact with databases (like MySQL) to retrieve, update, and manage data.
  • Compatibility: PHP is compatible with various databases and can be used on multiple operating systems, making it a versatile choice for web development.

Example of a PHP script:

				
					<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  }
} else {
  echo "0 results";
}
$conn->close();
?>

				
			

Client-Side Scripting

Client-side scripting involves scripts that run on the user’s browser after the web page has been loaded. Common client-side scripting languages include JavaScript, HTML, and CSS. PHP is not used for client-side scripting, but understanding the role of client-side scripts is important.

How Client-Side Scripting Works:

  1. Request: A user requests a web page.
  2. Response: The server sends the HTML, CSS, and JavaScript to the user’s browser.
  3. Processing: The browser processes the scripts. JavaScript can manipulate the DOM, handle events, and perform asynchronous operations without reloading the page.

Advantages of Client-Side Scripting:

  • Interactivity: Client-side scripts can create interactive web pages that respond to user actions without needing to reload the page.
  • Reduced Server Load: Some processing tasks can be handled by the user’s browser, reducing the load on the web server.
  • Immediate Feedback: Client-side scripts can provide instant feedback to user actions, such as form validation.

Example of a JavaScript snippet:

				
					<!DOCTYPE html>
<html>
<head>
    <title>Client-Side Scripting Example</title>
</head>
<body>
    <h1>Welcome to My Website</h1>
    <button onclick="showMessage()">Click Me</button>
    <p id="message"></p>

    <script>
        function showMessage() {
            document.getElementById("message").innerHTML = "Hello, World!";
        }
    </script>
</body>
</html>

				
			

Integration of PHP and Client-Side Scripts

Often, server-side and client-side scripts work together to create dynamic and interactive web applications. For example, PHP can generate the initial HTML content, and JavaScript can handle user interactions on the client side.

Example of PHP generating JavaScript:

				
					<?php
$userName = "John Doe";
echo "<script>
    var userName = '$userName';
    alert('Welcome ' + userName);
</script>";
?>

				
			

Key Differences

1. Execution Location:

  • Server-side: PHP scripts are executed on the server.
  • Client-side: JavaScript and other client-side scripts are executed in the user’s browser.

2. Security:

  • Server-side: More secure since the code is not visible to the user.
  • Client-side: Less secure as the code is visible and can be manipulated by the user.

3. Interaction with Databases:

  • Server-side: PHP can interact directly with databases.
  • Client-side: JavaScript cannot interact directly with databases without server-side support (e.g., via AJAX calls to PHP scripts).
Scroll to Top