PHP Introduction
beginnerPart of PHP Basics
Theory
PHP (Hypertext Preprocessor) is a widely-used, open-source server-side scripting language designed specifically for web development. It can be embedded directly into HTML and is the backbone of many popular content management systems like WordPress, Drupal, and Joomla.
What is PHP?
PHP code is executed on the server, not in the browser. The server processes PHP code and sends the resulting HTML output to the client. This means users never see your PHP source code — they only see the HTML it generates.
How PHP Works (Request-Response)
Browser (Client) Server (PHP)
│ │
│──── HTTP Request ────────→ │
│ │── PHP Interpreter processes script
│ │── Executes code, queries DB, etc.
│ │── Generates HTML output
│←─── HTTP Response (HTML) ───│
│ │
│── Renders HTML in browser
This model allows PHP to:
- Generate dynamic content
- Read/write files on the server
- Process form data
- Connect to databases
- Manage sessions and cookies
Installing PHP (XAMPP/WAMP/MAMP)
For local development, use an all-in-one package:
- XAMPP (cross-platform): Includes PHP, MySQL, Apache, and phpMyAdmin
- WAMP (Windows): Apache, MySQL, PHP for Windows
- MAMP (macOS): Apache, MySQL, PHP for macOS
After installation, place your PHP files in the web server's document root:
- XAMPP:
C:/xampp/htdocs/ - Linux:
/var/www/html/
Start Apache and MySQL from the control panel, then access http://localhost/your-file.php in your browser.
PHP Tags
PHP code is embedded in HTML using opening and closing tags:
<!DOCTYPE html>
<html>
<body>
<h1>My Page</h1>
<?php
echo "Hello from PHP!";
?>
</body>
</html>Short echo tag (works if enabled):
<?= "Hello World" ?>echo and print
echo and print output text to the browser:
<?php
echo "Hello World"; // Most common
echo "Line 1", " ", "Line 2"; // echo accepts multiple parameters
print "Hello World"; // print returns 1, takes one argument
echo "<h1>HTML tags work too!</h1>";
?>Comments
<?php
// Single-line comment
# Also a single-line comment
/*
Multi-line comment
spanning multiple lines
*/
/**
* Docblock comment
* Used before functions and classes
*/
?>Basic Syntax
PHP scripts start with <?php and end with ?>. Files typically have a .php extension. Every statement ends with a semicolon (;).
<?php
// Variable declaration with $ prefix
$name = "Alice";
$age = 25;
// String concatenation with .
echo "Hello, " . $name . "!";
// Arithmetic
$sum = 10 + 20;
?>Variables ($ prefix)
All PHP variables start with the $ symbol. They are dynamically typed — you don't need to declare a type:
<?php
$name = "Alice"; // String
$age = 25; // Integer
$price = 19.99; // Float/Double
$isActive = true; // Boolean
$colors = array("red", "green", "blue"); // Array
$nothing = null; // Null
?>Variable names are case-sensitive ($name ≠ $Name).
PHP as a Web Language
PHP excels at web tasks:
- Form processing: Handles HTML form submissions
- Database access: Connects to MySQL, PostgreSQL, SQLite
- Sessions: Maintains user state across pages
- File handling: Uploads, reads, and writes files
- Email: Sends emails with the
mail()function - APIs: Creates and consumes REST APIs
- Templating: Generates dynamic HTML pages
Unlike JavaScript (client-side), PHP code runs on the server. You cannot see PHP source code by viewing page source in a browser — you only see the HTML output it produces.
Practical Examples
Use <?= $variable ?> as a shortcut for <?php echo $variable; ?>. It's cleaner and commonly used in templates. However, never use short tags (<?) without php — they depend on server configuration.
Exercises
Personal Info Page
Create a PHP page that stores your personal information in variables (name, age, city, profession) and displays them in a styled HTML card using echo and string concatenation.
Expected Output:
A styled card showing your name, age, city, and professionDynamic Greeting with Time
Create a PHP script that displays a different greeting based on the time of day (morning/afternoon/evening). Also display the current date in a custom format.
Expected Output:
Good morning/afternoon/evening based on the current time, plus the formatted dateFix the PHP Syntax
Fix the syntax errors in this PHP script. It should display a simple multiplication table.
Expected Output:
5 x 1 = 5\n5 x 2 = 10\n...\n5 x 10 = 50Mini Quiz
Mini Quiz
Mini Project
Mini Project: Personal Dashboard
Create a PHP-powered personal dashboard page that displays dynamic content: time-based greeting, current date, a randomly selected quote, server information, and a simple visitor counter stored in a file.
Requirements:
Bonus Challenge
Make the theme change based on the time of day (light theme for daytime, dark theme at night). Add a visit date log.