PHP Introduction
Understanding the fundamentals of PHP programming
🌐 What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language embedded in HTML. It processes code on the server and sends results to browsers, enabling dynamic web content creation.
<?php
// Simple PHP script
echo "Welcome to PHP!";
?>
Output:
Welcome to PHP!
Key PHP Features
Server-Side
PHP runs on the server, not in the browser. The server processes PHP code and sends HTML to the client, keeping your logic secure.
<?php
echo "Processed on server";
?>
Database Integration
PHP easily connects to databases like MySQL, PostgreSQL, and MongoDB. Retrieve, store, and manage data efficiently for dynamic websites.
<?php
$conn = mysqli_connect(
"localhost", "user", "pass"
);
?>
Cross-Platform
PHP works on Windows, Linux, macOS, and Unix. Write once, deploy anywhere. Compatible with Apache, Nginx, and IIS servers.
<?php
// Works everywhere
phpinfo();
?>
Rich Extensions
Thousands of built-in functions and extensions available. Handle images, PDFs, emails, sessions, cookies, and more without external libraries.
<?php
mail("[email protected]",
"Subject", "Message");
?>
🔹 How PHP Works
Understanding the PHP execution process:
- Client Request: User visits a PHP page in their browser
- Server Processing: Web server sends the file to PHP interpreter
- Code Execution: PHP processes the code and generates HTML
- Response: Server sends the HTML result back to the browser
- Display: Browser displays the final HTML page
<!DOCTYPE html>
<html>
<body>
<h1>Server Time</h1>
<?php
// This runs on the server
echo "<p>Current time: " . date("h:i:s A") . "</p>";
?>
</body>
</html>
Output (example):
Server Time
Current time: 02:45:30 PM
🔹 PHP File Structure
PHP files have a .php extension and can contain HTML, CSS, JavaScript, and PHP code:
<!DOCTYPE html>
<html>
<head>
<title>PHP Page</title>
<style>
.highlight { color: blue; }
</style>
</head>
<body>
<h1>Mixed Content Example</h1>
<!-- HTML content -->
<p>This is regular HTML.</p>
<?php
// PHP code block
$message = "This is from PHP!";
echo "<p class='highlight'>$message</p>";
?>
<script>
// JavaScript still works
console.log("JavaScript active");
</script>
</body>
</html>
Output:
Mixed Content Example
This is regular HTML.
This is from PHP!
🔹 PHP vs Other Languages
How PHP compares to other web technologies:
PHP vs JavaScript:
- PHP: Runs on server, handles backend logic, database operations
- JavaScript: Runs in browser, handles frontend interactions, animations
- Together: PHP generates data, JavaScript makes it interactive
PHP vs Python:
- PHP: Built specifically for web, easier web hosting, more web frameworks
- Python: General-purpose, better for data science, machine learning
PHP vs Node.js:
- PHP: Mature ecosystem, easier deployment, synchronous by default
- Node.js: JavaScript everywhere, asynchronous, real-time applications
🔹 Real-World PHP Usage
Major websites and platforms built with PHP:
Started with PHP and still uses it extensively with their custom HipHop compiler.
WordPress
Powers 40%+ of all websites globally, entirely built with PHP.
Wikipedia
MediaWiki software running Wikipedia is written in PHP.
Slack
Uses PHP for backend services handling millions of messages.
🔹 Your First PHP Script
Let's create a simple interactive PHP script:
<?php
// Get current information
$dayOfWeek = date("l");
$currentYear = date("Y");
$randomNumber = rand(1, 100);
// Display information
echo "<h2>PHP Information</h2>";
echo "<p>Today is: $dayOfWeek</p>";
echo "<p>Current year: $currentYear</p>";
echo "<p>Random number: $randomNumber</p>";
// Simple calculation
$sum = 10 + 20;
echo "<p>10 + 20 = $sum</p>";
?>
Output (example):
PHP Information
Today is: Wednesday
Current year: 2025
Random number: 47
10 + 20 = 30