PHP: The Server Pulse
Understand the engine that powers the world's most popular websites.
Chapter 1: The Secret Kitchen
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 ?>.
<p>This is normal HTML.</p>
<?php
// This is secret PHP code!
// The user will never see this line.
?>
Chapter 2: The Dollar Sign
You do not need to use words like let or const. You just type a $ and give the box a name.
<?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
$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)
- 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
$message = "Welcome!"; // String
$age = 15; // Integer
$price = 9.99; // Float
$isRaining = false; // Boolean
?>
Chapter 4: Kitchen Math
You can use the basic symbols to do math:
+for adding-for subtracting*for multiplying (a star)/for dividing (a slash)
<?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
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
$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
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
// 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
$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
To create a recipe, use the word function. Give it a name. Then put your code inside the curly brackets.
<?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
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
To make a list, you can use the word array() or just square brackets []. You separate items with commas.
<?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
// 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
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $singleColor) {
echo "I love the color " . $singleColor . "<br>";
}
?>
Chapter 9: The Super Boxes
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
// 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
When an HTML form uses method="POST", the data travels silently across the internet. PHP catches it using a magic Super Box called $_POST.
<!-- 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
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
// 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)
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
// 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.