PHP Tutorial menu

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

  1. The visitor opens a page such as shop.php in the browser.
  2. The browser sends a request to the web server.
  3. The server runs the PHP code in that file.
  4. PHP produces plain HTML.
  5. 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?

TaskExample
Create pagesShow today's date or the logged-in user's name
Handle formsRead an admission form and save it
Use databasesStore products, orders and students in MySQL
Manage loginsRemember a user with sessions and cookies
Work with filesUpload 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.

  1. Download XAMPP from the Apache Friends website and install it.
  2. Open the XAMPP Control Panel and click Start next to Apache.
  3. Find the folder C:\xampp\htdocs.
  4. Create a folder there, for example myproject.
  5. 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

bash
php -v
php -S localhost:8000

The 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

php
<!DOCTYPE html>
<html>
<body>

<h1>My First PHP Page</h1>
<?php
echo "Hello from the server!";
?>

</body>
</html>
Output
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
<?php
echo "Today is " . date("l") . ".";
echo "<br>";
echo "The year is " . date("Y") . ".";
Output
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?

Learn PHP with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp