Introduction
When you open a website, the page you see is HTML. But somebody had to build that HTML — fetch the student's name from the database, check the password, decide what to show. On most of the web, that somebody is PHP.
PHP is a server-side language. That means PHP code never runs in your browser. It runs on the server: the browser asks for a page, PHP thinks, builds plain HTML, and sends only the finished HTML back. Your browser never sees the PHP code at all — and that is why passwords and database details stay safe.
PHP powers a huge part of the web — WordPress, Facebook's early days, and Laravel, the framework you will meet in the next phase. Learn PHP first and Laravel becomes easy, because Laravel is written in PHP.
(Roman Urdu: PHP server par chalta hai — browser ko sirf tayyar HTML milta hai, PHP code kabhi nazar nahi aata. Isi liye Laravel se pehle PHP seekhna zaroori hai.)
To run PHP on your own computer you need a local server. The easy way is XAMPP — one free install that gives you Apache (the server), MySQL (the database) and PHP together. You put your files in XAMPP's htdocs folder and open them at http://localhost/your-file.php.
Key points
- PHP stands for "PHP: Hypertext Preprocessor" — a language that prepares web pages before they reach the browser.
- Server-side means the code runs on the server, not in the browser. The browser only ever receives finished HTML.
- A
.phpfile can hold HTML and PHP together. PHP parts sit between<?phpand?>tags; everything outside the tags is plain HTML. echoprints text into the page.echo "Hello";sends the word Hello into the HTML.- XAMPP is the beginner's local server. Install it once and you get Apache + MySQL + PHP in one package.
- Your files live in
htdocs. On Windows that is usuallyC:\xampp\htdocs; on Mac it is/Applications/XAMPP/htdocs. - Open PHP pages through
http://localhost/..., never by double-clicking. Double-clicking opens the file, not the server — and PHP will not run. - PHP is free and open source. No licence, no payment, and it runs on almost every web host in the world.
- PHP is Laravel's base language. Every Laravel file you will write later is a PHP file first.
Examples
Example 1 — Your first PHP page
Save this as hello.php inside htdocs, start Apache in the XAMPP control panel, then open http://localhost/hello.php:
<?php
echo "Hello, PHP!";
?>What you will get:
Hello, PHP!The browser shows only the words. Right-click the page and choose View Source — you will see Hello, PHP! and no PHP code at all. The server already ran it.
Example 2 — PHP inside HTML
A PHP file is mostly a normal HTML page with small PHP islands inside it:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome</h1>
<p>Today is <?php echo date("l"); ?>.</p>
</body>
</html>What you will get:
Welcome
Today is Tuesday.date("l") is a built-in PHP function that gives the full name of today's day. The day changes by itself — no editing needed.
Example 3 — PHP does the maths before the page loads
<?php
$price = 250;
$qty = 3;
$total = $price * $qty;
echo "Total: ";
echo $total;
?>What you will get:
Total: 750The browser never sees $price * $qty. It only receives Total: 750. That is the whole idea of server-side code.
Example 4 — Checking that PHP is alive
A one-line file that proves your server works — every beginner should run this first:
<?php
phpinfo();
?>What you will get:
A long page showing your PHP version and every setting.If you see that page, XAMPP and PHP are running. If you see the raw code instead, Apache is not running — start it in the XAMPP control panel.
Example 5 — A comment the browser never sees
<?php
// This line is a note for humans only. PHP skips it.
echo "Hi";
/* This is a longer note.
It can span many lines. */
echo " there";
?>What you will get:
Hi thereComments explain your code to the next person who reads it. The browser never receives them.
Quick recap
- PHP runs on the server and sends finished HTML to the browser.
- PHP code lives between
<?phpand?>inside a.phpfile. echoprints text into the page;date("l")gives today's day name.- XAMPP gives you Apache + MySQL + PHP; files go in
htdocs. - Open pages through
http://localhost/..., never by double-clicking. - PHP is free, and it is the language Laravel is built on.
Common mistakes
- Double-clicking the .php file — the browser shows raw code or downloads the file. Fix: start Apache, then open
http://localhost/your-file.php. - Forgetting to start Apache in XAMPP — the browser says the site cannot be reached. Fix: open the XAMPP control panel and press Start next to Apache.
- Saving the file outside htdocs —
http://localhostcannot see it. Fix: save every PHP file inside thehtdocsfolder. - Writing PHP in a .html file — the server treats it as plain HTML and the code shows on the page. Fix: the file must end in
.php. - Forgetting the closing
?>when HTML follows — PHP swallows the HTML as code and errors. Fix: close the tag before the HTML starts (a file that is pure PHP may skip the closing tag). - Expecting the browser to show PHP errors nicely — a blank white page usually means a PHP error with display turned off. Fix: check the XAMPP Apache error log, or add
error_reporting(E_ALL);at the top while learning.
Practice
- Install XAMPP, start Apache, and open
http://localhost— write down what you see. - Create
htdocs/hello.phpwithecho "Hello, PHP!";and open it athttp://localhost/hello.php. - View Source on that page and confirm no PHP code is visible to the browser.
- Make a page that prints today's day with
date("l")and the year withdate("Y"). - Create
htdocs/info.phpwithphpinfo();and find your PHP version number on the page. - Deliberately open
hello.phpby double-clicking it in the folder — write down, in one line, what happens and why. - Write a page that echoes your name inside an
<h1>and a sentence about yourself inside a<p>.
Practice quiz
Tap an answer to check it — a right answer turns green, a wrong one turns red and shows the right answer. Nothing is scored or saved; this is just for practice.
1. Where does PHP code run?
2. What does XAMPP give you in one install?
3. How do you open a PHP page on your own computer?