Imagine a restaurant. HTML is the menu, CSS is the decoration, but PHP is the chef in the kitchen! It works behind the scenes to cook up websites, handle logins, and save data. Ready to cook some code? 👨🍳
PHP is a superpower that runs on the Server (the big computers that host websites). It's like the brain of a website! 🧠
It can do math, make decisions, and build pages before the user sees them.
WordPress, Wikipedia, and millions of other sites use PHP!
PHP can talk to databases to save user scores, posts, and profiles.
It was made for the web, so it's very friendly and fun to write!
PHP code lives inside special secret tags: <?php and ?>. This tells the computer, "Hey, this is PHP code, not just regular HTML!" 🤫
The word echo is like the chef shouting the order out loud. It tells PHP to print something on the screen! 📢
<?php
// This is a comment. The computer ignores it!
echo "Hello, World! 🌍";
echo "Welcome to PHP!";
$name = "Smart Kid";
echo "Hello " . $name;
?>
Hello, World! 🌍 Welcome to PHP! Hello Smart Kid
echo) MUST end with a semicolon ; . It's like putting a period at the end of a sentence!A Variable is like a labeled jar where you can store stuff. In PHP, every jar starts with a dollar sign $. 💵
<?php
$student_name = "Aarav"; // A text jar
$age = 13; // A number jar
$is_active = true; // A true/false jar
echo "Student: " . $student_name;
echo "Age: " . $age;
echo "Is active? " . $is_active;
?>
Student: Aarav Age: 13 Is active? 1
true is often shown as a "1" and false is shown as nothing (or "0"). It's just how computers think!Just like you have different toys for different games, PHP has different types of data! 🧸
<?php
$fruit = "Apple"; // String
$price = 50; // Integer
$weight = 1.5; // Float
$is_tasty = true; // Boolean
$colors = ["Red", "Blue"]; // Array
echo "Fruit: " . $fruit;
echo "Price: " . $price;
echo "First color: " . $colors[0];
?>
Fruit: Apple Price: 50 First color: Red
PHP can make decisions! If it's raining, take an umbrella. Else, wear sunglasses! ☔😎
<?php
$marks = 85;
echo "Your marks: $marks\n";
echo "Grade: ";
if ($marks >= 90) {
echo "A+ (Outstanding!) 🌟";
} elseif ($marks >= 80) {
echo "A (Excellent!) 👍";
} elseif ($marks >= 40) {
echo "Pass 🙂";
} else {
echo "Fail 😢";
}
?>
Your marks: 85 Grade: A (Excellent!) 👍
$marks to 50. What grade would you get?A Function is a reusable block of code. It's like writing down a recipe once, and then just saying "Make Pizza!" whenever you want one! 🍕
<?php
// Writing the recipe
function greet($name) {
return "Hello, " . $name . "! 👋";
}
function addNumbers($a, $b) {
return $a + $b;
}
// Using the recipe
echo greet("Riya");
echo "5 + 3 = " . addNumbers(5, 3);
?>
Hello, Riya! 👋 5 + 3 = 8
PHP is amazing at handling HTML forms. When you click "Submit" on a website, PHP is the chef that receives your order! 📝
<?php
// Pretend a user submitted this form
$_POST['username'] = "john_doe";
$_POST['email'] = "john@example.com";
// PHP grabs the order
$user = $_POST['username'];
$email = $_POST['email'];
echo "Order received! 📦";
echo "Username: " . $user;
echo "Email: " . $email;
?>
Order received! 📦 Username: john_doe Email: john@example.com
$_POST is a special PHP bag that holds everything a user typed into a form!Let's put it all together! We'll use PHP variables, arrays, and if/else to make a simple report card system for students! 📚
<?php
// Student data in an array
$student = [
"name" => "Aarav",
"marks" => 78
];
// Function to decide grade
function getGrade($marks) {
if ($marks >= 90) return "A+ 🌟";
elseif ($marks >= 80) return "A 👍";
elseif ($marks >= 40) return "Pass 🙂";
else return "Fail 😢";
}
// Let's print the report card!
echo "══════════════════════════\n";
echo " 📜 REPORT CARD 📜\n";
echo "══════════════════════════\n";
echo "Name: " . $student["name"] . "\n";
echo "Marks: " . $student["marks"] . "/100\n";
echo "Grade: " . getGrade($student["marks"]) . "\n";
echo "══════════════════════════\n";
?>
══════════════════════════ 📜 REPORT CARD 📜 ══════════════════════════ Name: Aarav Marks: 78/100 Grade: Pass 🙂 ══════════════════════════
Let's see how much you learned! Pick the best answer. 🧠
Check the boxes as you finish each lesson. Watch your progress bar fill up! 🌈
Awesome job reaching here! 🎉 You just learned the basics of PHP. Here are fun things to do next: