-
1. What is PHP?
PHP stands for Hypertext Preprocessor. It is a popular programming language that is used to build websites and web applications. If you have ever filled out a login form, submitted a contact form, or browsed an online shopping site — there is a good chance PHP was working behind the scenes!
PHP is a server-side language. This means the code runs on the web server, not in the user's browser. The server processes your PHP code and then sends the final result (usually HTML) to the browser.
? Note: Think of PHP like a chef in a kitchen. The user orders food (makes a request), the chef (PHP) prepares it in the kitchen (server), and then serves the finished dish (HTML) to the user's table (browser).
Why Learn PHP?
• PHP is free and open-source — no cost to use!
• It powers over 77% of all websites on the internet (including WordPress!).
• It is beginner-friendly and has a large community for help.
• It connects easily with databases like MySQL.
• It runs on all major platforms — Windows, Mac, and Linux.
2. Setting Up PHP on Your Computer
Before writing PHP code, you need to set up your local environment. The easiest way is to install XAMPP — a free software package that gives you everything you need in one click.
What is XAMPP?
XAMPP is a package that includes Apache (a web server), PHP, and MySQL (a database). It creates a mini web server right on your laptop or desktop computer.
Steps to Get Started:
1. Go to https://www.apachefriends.org and download XAMPP.
2. Install it and then open the XAMPP Control Panel.
3. Click Start next to Apache to start the web server.
4. Navigate to the htdocs folder inside your XAMPP directory and create your PHP files there.
5. Open your browser and go to http://localhost/yourfile.php to see your page.
✅ Tip: You can use any text editor like VS Code, Notepad++, or even Notepad to write PHP code.
3. Your First PHP Program
Every PHP file ends with the .php extension. PHP code is always written inside special tags that tell the server where the PHP starts and ends.
Basic PHP Structure:
<?php
// Your PHP code goes here
?>
The <?php tag is the opening tag. The ?> tag is the closing tag. Everything between these two tags is PHP code.
Hello, World! — Your First Program
Let us write the classic first program. Create a file called hello.php and type this:
<?php
echo "Hello, World!";
?>
Open your browser and go to http://localhost/hello.php — you should see Hello, World! displayed on the screen. Congratulations! You just wrote your first PHP program!
? Note: echo is the PHP command used to display text on the screen. It is one of the most commonly used commands in PHP.
4. Variables in PHP
A variable is like a box where you store information. In PHP, all variable names start with a dollar sign ($). You can store text, numbers, or other types of data inside a variable.
How to Create a Variable:
<?php
$name = "Alice";
$age = 20;
echo "My name is " . $name . " and I am " . $age . " years old.";
?>
Output: My name is Alice and I am 20 years old.
? Note: The dot (.) symbol in PHP is used to join (concatenate) two strings together, like gluing pieces of text.
Types of Data in PHP
Data Type
Example
Description
String
$name = "Alice"
Text or characters
Integer
$age = 20
Whole numbers
Float
$price = 9.99
Decimal numbers
Boolean
$isLoggedIn = true
True or False
Array
$colors = ["red","blue"]
A list of values
5. Making Decisions with If / Else
In real life, we make decisions all the time. If it is raining, take an umbrella. Otherwise, leave it at home. PHP lets you do the same thing in your code using if and else statements.
Basic If / Else Example:
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult!";
} else {
echo "You are a minor.";
}
?>
Output: You are an adult!
? Note: The condition inside the if() must be true for the first block to run. If it is false, the else block runs instead.
Comparison Operators Cheat Sheet:
• == means is equal to
• != means is not equal to
• > means is greater than
• < means is less than
• >= means is greater than or equal to
• <= means is less than or equal to
Adding More Conditions with elseif:
<?php
$score = 75;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 70) {
echo "Grade: B";
} elseif ($score >= 50) {
echo "Grade: C";
} else {
echo "Grade: Fail";
}
?>
Output: Grade: B
6. Repeating Things with Loops
Loops let you run the same block of code multiple times without copy-pasting. Imagine printing numbers from 1 to 100 — you would not write echo 100 times! A loop does it for you automatically.
The for Loop
Use a for loop when you know exactly how many times you want to repeat something.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: " . $i . "<br>";
}
?>
Output: Number: 1, Number: 2, Number: 3, Number: 4, Number: 5 (each on a new line).
? Note: The for loop has 3 parts inside the brackets: start ($i = 1), condition ($i <= 5), and increment ($i++). $i++ means add 1 to $i after each loop.
The while Loop
Use a while loop when you want to keep looping as long as a condition is true.
<?php
$count = 1;
while ($count <= 3) {
echo "Count is: " . $count . "<br>";
$count++;
}
?>
Output: Count is: 1, Count is: 2, Count is: 3.
✅ Tip: Always make sure your loop has a way to stop, otherwise it will run forever and crash your server!
The foreach Loop
The foreach loop is perfect for going through arrays (lists of items).
<?php
$fruits = ["Apple", "Banana", "Mango", "Orange"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
Output: Apple, Banana, Mango, Orange (each on a new line).
7. Functions — Write Once, Use Many Times
A function is a block of code that you write once and can use (call) as many times as you want. Think of it as a recipe — you write the recipe once and can cook that dish whenever you like.
Creating and Calling a Function:
<?php
// Define the function
function greet($name) {
echo "Hello, " . $name . "! Welcome to PHP!<br>";
}
// Call the function
greet("Alice");
greet("Bob");
greet("Charlie");
?>
Output: Hello, Alice! Welcome to PHP! / Hello, Bob! Welcome to PHP! / Hello, Charlie! Welcome to PHP!
Functions That Return a Value:
<?php
function addNumbers($a, $b) {
return $a + $b;
}
$result = addNumbers(10, 25);
echo "The sum is: " . $result;
?>
Output: The sum is: 35
? Note: return sends a value back from the function. You can then store it in a variable or use it directly.
8. Arrays — Working with Lists
An array lets you store multiple values in a single variable. Instead of creating $color1, $color2, $color3, you can use one array to hold them all.
Indexed Arrays (Using Numbers):
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0]; // Output: Red
echo $colors[1]; // Output: Green
echo $colors[2]; // Output: Blue
?>
? Note: Arrays start counting from 0, not 1. So the first item is at index [0], the second is at [1], and so on.
Associative Arrays (Using Names as Keys):
Associative arrays use named keys instead of numbers, which makes them easier to understand.
<?php
$student = [
"name" => "Alice",
"age" => 20,
"grade" => "A"
];
echo "Name: " . $student["name"] . "<br>";
echo "Age: " . $student["age"] . "<br>";
echo "Grade: " . $student["grade"] . "<br>";
?>
Output: Name: Alice | Age: 20 | Grade: A
9. PHP and HTML Forms
One of the most exciting things about PHP is that it can collect data from HTML forms. When a user fills in a form and clicks Submit, PHP processes that information.
Step 1 — Create the HTML Form (form.html):
<form action="process.php" method="POST">
<label>Enter your name:</label>
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
Step 2 — Process the Form with PHP (process.php):
<?php
$name = $_POST["username"];
echo "Hello, " . $name . "! Nice to meet you!";
?>
When the user types their name and clicks Submit, PHP grabs the value using $_POST and displays a greeting!
✅ Tip: Always clean user input before using it to keep your website safe. Use htmlspecialchars($name) to prevent harmful code injection.
10. Quick Summary — What You Learned
Topic
Key Takeaway
What is PHP?
A server-side language for building websites and web apps.
Setup
Install XAMPP to run PHP locally on your computer.
echo
Used to display text or variables on the screen.
Variables
Boxes to store data — always start with $ sign.
If / Else
Used to make decisions based on conditions.
Loops
Repeat code multiple times automatically (for, while, foreach).
Functions
Reusable blocks of code — define once, call many times.
Arrays
Store multiple values in a single variable.
Forms
Collect user input using HTML forms and process with PHP.
This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies.