PHP: The Server Pulse

Understand the engine that powers the world's most popular websites.

Chapter 1: The Secret Kitchen

HTML, CSS, and JS make the beautiful dining room. But where does the food come from? It comes from the hidden kitchen in the back. That kitchen is called PHP.

PHP is a language that runs on a "Server". A server is just a giant computer far away.

When you click a link, your browser asks the server for the page. PHP wakes up in the kitchen, cooks the HTML page using data, and sends the finished HTML back to you. You never see the PHP code. You only see the finished food!

Opening the Kitchen Door

To tell the server "Hey! Stop reading HTML and start reading PHP!", we have to use a special, secret tag. You open the door with <?php and close it with ?>.

php
<p>This is normal HTML.</p>

<?php
    // This is secret PHP code!
    // The user will never see this line.
?>

Chapter 2: The Dollar Sign

Just like in JavaScript, PHP needs boxes to store memories. But PHP is very strict. Every single memory box must start with a dollar sign.

You do not need to use words like let or const. You just type a $ and give the box a name.

php
<?php
    $heroName = "Arthur";
    $goldCoins = 100;
?>

Echoing Words

PHP does its work in secret. How does it push words onto the HTML page for the user to see? It echoes them! We use the word echo to print words onto the screen.

php
<?php 
    $playerName = "Sam";
    echo "Hello, " . $playerName; 
?>

Did you notice the little dot (.)? In PHP, we use a dot to glue words together! It is like using the + sign in JavaScript.

Chapter 3: Ingredients (Types)

You can put many different types of ingredients inside your memory boxes. Let us look at the most common types.
  • String (Text): Words or letters wrapped in quotes. Example: "Super Cat"
  • Integer (Whole Number): A number without a decimal. Example: 42
  • Float (Decimal Number): A number with a decimal. Example: 3.14
  • Boolean (Yes/No): True or False. Example: true
  • Array (List): A box that holds multiple things at once. We will talk more about this later!
php
<?php
    $message = "Welcome!";     // String
    $age = 15;                 // Integer
    $price = 9.99;             // Float
    $isRaining = false;        // Boolean
?>

Chapter 4: Kitchen Math

The server is just a giant calculator. It loves doing math. Let us show it how to add, subtract, and compare things.

You can use the basic symbols to do math:

  • + for adding
  • - for subtracting
  • * for multiplying (a star)
  • / for dividing (a slash)
php
<?php
    $apples = 5;
    $oranges = 3;

    // We can add them together!
    $totalFruit = $apples + $oranges; // This makes 8
?>

You can also ask the server questions. Like, "Is 10 bigger than 5?" The server will answer with true or false.

  • == means "Equal to"
  • != means "Not equal to"
  • > means "Bigger than"
  • < means "Smaller than"

Chapter 5: Making Choices

A kitchen is not very smart if it always cooks the exact same meal. We need to teach the server how to make a choice. "If the user is an admin, show the secret page. Else, show the normal page."

We use an if statement. You put your question inside round brackets (). If the answer is true, the server runs the code inside the curly brackets {}.

php
<?php
    $score = 100;

    if ($score > 50) {
        // This will happen because 100 IS bigger than 50!
        echo "You win the game!";
    } else {
        // This happens if the score is low.
        echo "Try again!";
    }
?>

Chapter 6: Doing it Again

What if you want to say "Hello" five times? You could type it five times. But what if you want to say it one hundred times? You would get very tired. The server never gets tired.

The For Loop

We use a loop to repeat things. A for loop needs three things: a starting point, an ending rule, and a step size.

php
<?php
    // Start at 1. Keep going while $i is less than or equal to 5. Add 1 each time.
    for ($i = 1; $i <= 5; $i++) {
        echo "Hello number " . $i . "<br>";
    }
?>

The While Loop

A while loop is simpler. It just says: "Keep repeating this code AS LONG AS this rule is true."

php
<?php
    $health = 3;
    
    while ($health > 0) {
        echo "You are still alive!<br>";
        $health--; // This subtracts 1 from health each time
    }
    
    echo "Game Over.";
?>

Chapter 7: Special Recipes

If you cook the same meal every day, it is annoying to read the whole recipe every time. You should wrap your recipe up, give it a name, and just call its name whenever you need it. This is a Function.

To create a recipe, use the word function. Give it a name. Then put your code inside the curly brackets.

php
<?php
    // Creating the recipe
    function sayGoodMorning() {
        echo "Good morning!<br>";
        echo "Time to wake up!<br>";
    }

    // Cooking the recipe!
    sayGoodMorning();
?>

Recipes can also take ingredients. If you want a recipe to add any two numbers together, you give it placeholders inside the round brackets.

php
<?php
    function addNumbers($number1, $number2) {
        $result = $number1 + $number2;
        echo $result;
    }

    // Cook the recipe with 5 and 3
    addNumbers(5, 3); // This will print 8!
?>

Chapter 8: The Magic Lists

Sometimes you have too many things to fit in one box. You do not want to make a new box for every friend you have. You just want a big list! We call this an Array.

To make a list, you can use the word array() or just square brackets []. You separate items with commas.

php
<?php
    $myFriends = ["Alex", "Sam", "Jordan"];
    
    // Computers always start counting at zero!
    echo $myFriends[0]; // This prints "Alex"
?>

Name Tags (Associative Arrays)

Sometimes counting by numbers (0, 1, 2) is confusing. What if you want to make a list describing a player? You can use name tags instead of numbers! We call this an Associative Array.

php
<?php
    // We use an arrow (=>) to connect the name tag to the value!
    $hero = [
        "name" => "Arthur",
        "score" => 100,
        "magic" => "Fire"
    ];

    echo $hero["name"]; // This prints "Arthur"
?>

The Foreach Loop

There is a special loop made just for reading lists. It is called foreach. It automatically goes through every single item in the list, one by one, until the list is empty.

php
<?php
    $colors = ["Red", "Blue", "Green"];

    foreach ($colors as $singleColor) {
        echo "I love the color " . $singleColor . "<br>";
    }
?>

Chapter 9: The Super Boxes

PHP has some magical, invisible boxes that are always available. They are called "Superglobals". They hold secret information about the user and the website.

Here are the two most common Super Boxes:

  • $_GET: This box holds any secret words typed into the website URL.
  • $_SERVER: This box holds secret info about the user's computer and the server itself.
php
<?php
    // If the URL is: website.com?name=Sam
    // PHP can automatically read the name "Sam" from the URL!
    
    echo "Welcome, " . $_GET['name']; 
?>

Chapter 10: Secret Forms

If a user types their secret password into an HTML form, we cannot send it through the URL using `$_GET`. Everyone would see it! We must send the password to the hidden PHP kitchen through the back door.

When an HTML form uses method="POST", the data travels silently across the internet. PHP catches it using a magic Super Box called $_POST.

php
<!-- The HTML Form -->
<form method="POST" action="/login.php">
    <input type="password" name="secret_code">
    <button type="submit">Enter</button>
</form>

<!-- Inside login.php (The Kitchen) -->
<?php
    // Catch the password silently!
    $password = $_POST['secret_code'];

    if ($password == "dragon") {
        echo "You may enter the castle.";
    } else {
        echo "Go away!";
    }
?>

Chapter 11: Remembering Guests

When you walk into a store, buy an apple, leave, and come back 5 minutes later, the cashier remembers you. But servers have terrible memories. Every time you click a link, the server forgets who you are! How do websites keep you "Logged In"?

We use Sessions. A Session is like giving the user a sticky note with a secret ID number on it. Every time they click a link, they show the sticky note to the server, and the server says "Ah! I remember you!"

To use a session, you MUST type session_start() at the very, very top of your PHP file.

php
<?php
    // Turn on the memory system!
    session_start();

    // The user logs in. We save their name in the Session Box.
    $_SESSION["user_name"] = "Arthur";
    
    // Now, even if they click to a completely different page...
    // As long as we use session_start(), PHP will remember them!
    echo "Welcome back, " . $_SESSION["user_name"];
?>

Chapter 12: Master Chefs (OOP)

As your kitchen gets bigger, having random variables and functions flying around gets messy. Professional chefs use Object-Oriented Programming (OOP). They create blueprints for things.

Imagine you want to make a game with lots of dogs. Instead of making variables for every single dog, you create a Class. A Class is a blueprint. It describes what a dog has (properties) and what a dog can do (methods).

php
<?php
    // 1. Create the Blueprint (The Class)
    class Dog {
        // What does a dog have?
        public $name;
        public $color;

        // What can a dog do?
        public function bark() {
            echo $this->name . " says WOOF!<br>";
        }
    }

    // 2. Build a real Dog using the blueprint (The Object)
    $myDog = new Dog();
    $myDog->name = "Buddy";
    $myDog->color = "Brown";

    // Make the dog bark!
    $myDog->bark(); // Prints: Buddy says WOOF!
?>

By creating Blueprints (Classes), you can easily build hundreds of real items (Objects) without rewriting your code. You are now a Master Chef!

The Engine Room is Yours

You have mastered PHP from A to Z! You know how to create variables, loop through arrays, handle secret forms, keep users logged in, and even build blueprints using OOP. The server is completely under your control.

End_of_Module_01
Return to Archives