๐ฏ Learning Objectives
- Understand what PHP is and how it works
- Install XAMPP server environment
- Learn PHP syntax and basic tags
- Work with PHP variables and data types
- Write your first PHP script
๐ What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language used to create dynamic web pages. Unlike HTML/CSS/JavaScript which run in the browser, PHP runs on the server and generates HTML that is sent to the browser.
Why Learn PHP?
- โ Powers 77% of websites (WordPress, Facebook, Wikipedia)
- โ Easy to learn for beginners
- โ Connects to databases (MySQL, PostgreSQL)
- โ Free and open-source
- โ Large community and resources
๐ป Installing XAMPP
Step 1: Download XAMPP
- Visit apachefriends.org
- Download XAMPP for your operating system (Windows/Mac/Linux)
- Choose the latest PHP version (PHP 8.x)
Step 2: Install XAMPP
- Run the installer
- Select components: Apache, MySQL, PHP, phpMyAdmin
- Choose installation directory (default: C:\xampp)
- Complete installation
Step 3: Start Apache Server
- Open XAMPP Control Panel
- Click "Start" next to Apache
- Apache should turn green
- Open browser and go to:
http://localhost - You should see XAMPP welcome page
๐ Project Folder Structure
C:\xampp\htdocs\
โโโ php-practice\
โโโ day1-intro.php
โโโ day2-arrays.php
โโโ day3-forms.php
โโโ ...
Important: All PHP files must be placed in
C:\xampp\htdocs\ folder to run on localhost.
๐ค PHP Syntax Basics
PHP Tags
<?php
// PHP code goes here
echo "Hello, World!";
?>
Embedding PHP in HTML
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>Welcome to PHP</h1>
<?php
echo "<p>This paragraph was generated by PHP!</p>";
echo "<p>Current date: " . date("Y-m-d") . "</p>";
?>
</body>
</html>
๐ PHP Variables
Variable Declaration
<?php
// Variables start with $ sign
$name = "Ahmad";
$age = 25;
$price = 99.99;
$isStudent = true;
// Display variables
echo "Name: " . $name;
echo "<br>";
echo "Age: " . $age;
?>
Variable Rules
- Must start with $ sign
- Variable name must start with letter or underscore
- Can only contain letters, numbers, and underscores
- Case-sensitive ($name โ $Name)
๐ข PHP Data Types
| Data Type | Description | Example |
|---|---|---|
| String | Text data | $name = "John"; |
| Integer | Whole numbers | $age = 25; |
| Float | Decimal numbers | $price = 99.99; |
| Boolean | True or False | $isActive = true; |
| Array | Multiple values | $colors = ["red", "blue"]; |
๐จ๏ธ Echo and Print
<?php
// Using echo
echo "Hello, World!";
echo "<br>";
// Multiple values with echo
echo "Name: ", $name, " Age: ", $age;
// Using print
print "Hello again!";
// Concatenation with dot (.)
echo "My name is " . $name . " and I am " . $age . " years old.";
?>
๐ฌ PHP Comments
<?php
// Single line comment
# Another single line comment
/*
Multi-line comment
Useful for longer explanations
*/
echo "Comments help document code";
?>
๐งฎ Basic Arithmetic in PHP
<?php
$num1 = 10;
$num2 = 5;
$sum = $num1 + $num2;
$difference = $num1 - $num2;
$product = $num1 * $num2;
$quotient = $num1 / $num2;
$remainder = $num1 % $num2;
echo "Sum: " . $sum . "<br>";
echo "Difference: " . $difference . "<br>";
echo "Product: " . $product . "<br>";
echo "Quotient: " . $quotient . "<br>";
echo "Remainder: " . $remainder . "<br>";
?>
๐ป Complete Example: First PHP Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My First PHP Page</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.card {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
h1 { color: #ff6b6b; }
</style>
</head>
<body>
<div class="card">
<h1>Welcome to PHP Programming!</h1>
<?php
// Personal Information
$firstName = "Ahmad";
$lastName = "Garba";
$age = 25;
$city = "Kano";
$occupation = "Web Developer";
// Display information
echo "<h2>Personal Information</h2>";
echo "<p><strong>Full Name:</strong> " . $firstName . " " . $lastName . "</p>";
echo "<p><strong>Age:</strong> " . $age . " years old</p>";
echo "<p><strong>City:</strong> " . $city . "</p>";
echo "<p><strong>Occupation:</strong> " . $occupation . "</p>";
// Calculations
$yearBorn = 2025 - $age;
echo "<p><strong>Year Born:</strong> " . $yearBorn . "</p>";
// Current date and time
echo "<hr>";
echo "<p><strong>Today's Date:</strong> " . date("l, F j, Y") . "</p>";
echo "<p><strong>Current Time:</strong> " . date("h:i:s A") . "</p>";
?>
</div>
</body>
</html>
๐งช Testing Your PHP Script
- Save file as
first.phpinC:\xampp\htdocs\php-practice\ - Make sure Apache is running in XAMPP
- Open browser and go to:
http://localhost/php-practice/first.php - You should see your PHP output
โ๏ธ Practice Exercises
- Create a PHP file that displays your name, age, and favorite color
- Calculate and display the area of a rectangle (length ร width)
- Display current year, month, and day using PHP
- Create variables for 3 products with prices and calculate total
- Display a greeting message based on your name variable
๐ Common Errors & Solutions
| Error | Solution |
|---|---|
| Parse error: syntax error | Check for missing semicolons (;) at end of statements |
| Undefined variable | Make sure variable is declared before using it |
| Page shows PHP code | Apache not running, or file doesn't have .php extension |
| localhost not working | Start Apache in XAMPP Control Panel |
โ Today's Deliverable
- โ XAMPP installed and Apache running
- โ Project folder created in htdocs
- โ first.php file with personal information
- โ At least 5 variables declared
- โ Display information using echo
- โ Include date/time display
- โ Screenshot of working PHP page in browser