๐Ÿ“… Week 9 - Day 1: Introduction to PHP & Environment Setup

Monday | Duration: 3 hours

๐ŸŽฏ Learning Objectives

๐ŸŒ 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?

๐Ÿ’ป Installing XAMPP

Step 1: Download XAMPP

  1. Visit apachefriends.org
  2. Download XAMPP for your operating system (Windows/Mac/Linux)
  3. Choose the latest PHP version (PHP 8.x)

Step 2: Install XAMPP

  1. Run the installer
  2. Select components: Apache, MySQL, PHP, phpMyAdmin
  3. Choose installation directory (default: C:\xampp)
  4. Complete installation

Step 3: Start Apache Server

  1. Open XAMPP Control Panel
  2. Click "Start" next to Apache
  3. Apache should turn green
  4. Open browser and go to: http://localhost
  5. 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

๐Ÿ”ข 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

  1. Save file as first.php in C:\xampp\htdocs\php-practice\
  2. Make sure Apache is running in XAMPP
  3. Open browser and go to: http://localhost/php-practice/first.php
  4. You should see your PHP output

โœ๏ธ Practice Exercises

  1. Create a PHP file that displays your name, age, and favorite color
  2. Calculate and display the area of a rectangle (length ร— width)
  3. Display current year, month, and day using PHP
  4. Create variables for 3 products with prices and calculate total
  5. 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

โ† Previous Course Home Next: Day 2 โ†’