PHP Introduction
PHP is a server-side scripting language used to build dynamic websites.
What is PHP?
- PHP stands for PHP: Hypertext Preprocessor (a name that refers to itself)
- PHP code runs on the web server, not in the browser
- PHP can create HTML, read forms, send emails, and talk to databases
- PHP files end with
.php - PHP is free and open source
A normal HTML page is the same for every visitor. A PHP page can change: it can show "Good morning, Ali" to Ali and a different list of products to each shop customer.
How PHP Works
- The visitor opens a page such as
shop.phpin the browser. - The browser sends a request to the web server.
- The server runs the PHP code in that file.
- PHP produces plain HTML.
- The server sends that HTML back to the browser.
The visitor only sees the final HTML. Your PHP code, passwords and database details stay on the server.
What Can PHP Do?
| Task | Example |
|---|---|
| Create pages | Show today's date or the logged-in user's name |
| Handle forms | Read an admission form and save it |
| Use databases | Store products, orders and students in MySQL |
| Manage logins | Remember a user with sessions and cookies |
| Work with files | Upload a CV or a profile picture |
Installing PHP on Your Computer
The easiest way for beginners on Windows is XAMPP. It installs Apache (a web server), PHP and MySQL together.
- Download XAMPP from the Apache Friends website and install it.
- Open the XAMPP Control Panel and click Start next to Apache.
- Find the folder
C:\xampp\htdocs. - Create a folder there, for example
myproject. - Save your PHP files inside it and open
http://localhost/myproject/in your browser.
On macOS or Linux you can also install PHP and use its built-in server:
Example
php -v
php -S localhost:8000The first command shows your PHP version. The second starts a small test server in the current folder.
Always open PHP files through http://localhost/.... If you double-click a .php file, the browser shows the code instead of running it.
Your First PHP Script
Create a file named hello.php with this code:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First PHP Page</h1>
<?php
echo "Hello from the server!";
?>
</body>
</html>My First PHP Page Hello from the server!
PHP code is written between <?php and ?>. The echo statement sends text to the page.
PHP Can Show Changing Data
Example
<?php
echo "Today is " . date("l") . ".";
echo "<br>";
echo "The year is " . date("Y") . ".";Today is Thursday. The year is 2026.
Each time someone opens the page, PHP runs again and shows the current day and year. Your output will show the day you run it.
Exercise
Exercise
Which file name will a web server run as PHP?
