PHP

September 8, 2025 (1mo ago)

PHP (Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. This guide will walk you through the fundamental concepts of PHP.


1. Client-Side vs. Server-Side Scripting

Web development involves two main areas: the client-side (what you see in your browser) and the server-side (the backend logic).

  • Client-side scripting (like JavaScript) runs in the user's browser. It's used for interactive elements and manipulating the webpage in real-time.
  • Server-side scripting (like PHP) runs on the web server. It's used for tasks like accessing databases, handling form data, and managing user sessions before the page is sent to the browser.

2. Static vs. Dynamic Websites

  • Static websites are made of fixed content. Each page is an HTML file, and everyone who visits sees the exact same thing.
  • Dynamic websites can show different content to different users. They are powered by server-side languages like PHP that can generate HTML content on the fly, often by pulling data from a database.

3. Development Environment

To start writing and running PHP code, you need a local development environment. A popular choice is XAMPP, which bundles:

  • Apache: A web server to host your files.
  • MySQL: A database management system.
  • PHP: The PHP interpreter itself.

Once you install XAMPP, you can place your PHP files in the htdocs directory and access them through localhost in your browser.


4. PHP Syntax

PHP scripts are executed on the server. A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with ?>.

<?php
// This is a single-line comment
echo "Hello, World!";
?>

Variables: Variables in PHP start with a $ sign, followed by the name of the variable.

<?php
$greeting = "Hello";
$name = "Rifky";
echo $greeting . ", " . $name . "!"; // Outputs: Hello, Rifky!
?>

5. Arrays

An array is a special variable that can hold more than one value at a time. PHP supports several types of arrays:

  • Indexed arrays: Arrays with a numeric index.

    <?php
    $cars = array("Volvo", "BMW", "Toyota");
    echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
    ?>
  • Associative arrays: Arrays that use named keys that you assign to them.

    <?php
    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
    echo "Peter is " . $age['Peter'] . " years old.";
    ?>

6. Request Methods (GET & POST)

PHP is often used to handle data submitted from HTML forms. The two most common methods for sending form data are GET and POST.

  • GET: Form data is sent as part of the URL. It's visible in the address bar and has limits on the amount of information to send.
  • POST: Form data is sent in the HTTP request body. It's not visible in the URL, making it more secure for sensitive information.

You can access the data using PHP's superglobal variables $_GET and $_POST.

Example of a simple form:

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

And the PHP to handle it (welcome.php):

<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo "Welcome, " . $name . "!<br>";
echo "Your email address is: " . $email;
?>

7. Functions in PHP

Functions are reusable blocks of code that perform a specific task. Using functions helps make your code more organized, easier to debug, and reduces repetition.

  • Built-in Functions: PHP comes with thousands of built-in functions. For example, you can use date() to get the current date and time.

    <?php
    echo "Today is " . date("l, d F Y"); 
    // Outputs something like: Today is Monday, 08 September 2025
    ?>
  • User-Defined Functions: You can also create your own functions to suit your needs.

    <?php
    function greet($name) {
      return "Hello, " . $name . "!";
    }
     
    echo greet("Rifky"); // Outputs: Hello, Rifky!
    ?>

8. Advanced Arrays

While we've covered indexed and associative arrays, PHP also supports multidimensional arrays, which are arrays containing other arrays.

  • Multidimensional Arrays: These are useful for storing data in a table-like structure.
    <?php
    $students = [
        ["Rifky", "A", 95],
        ["Ben", "B", 80],
        ["Joe", "A", 92]
    ];
     
    echo $students[0][0] . " got grade " . $students[0][1]; 
    // Outputs: Rifky got grade A
    ?>

9. Superglobals

PHP has several predefined variables that are always accessible, regardless of scope. These are called "superglobals." We've already used $_GET and $_POST. Another useful one is $_SERVER, which holds information about headers, paths, and script locations.

<?php
// Check if the form has been submitted using the POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Process form data
  $name = $_POST['name'];
  echo "Hello, " . $name;
}
?>

10. Connecting to a Database with MySQLi

Most dynamic websites need a database to store information. PHP can connect to and interact with databases like MySQL. The MySQLi extension (MySQL Improved) is a common way to do this.

To connect to your local database, you can use the mysqli_connect() function. It typically requires the server name, username, password, and database name.

<?php
// Establish a connection to the database
$conn = mysqli_connect("localhost", "root", "", "phpdasar");
 
// Check the connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>

This covers the very basics to get you started with PHP. From here, you can explore more advanced topics like functions, file handling, and connecting to databases. Happy coding!