You log in to an online shop, and the corner of the page says "Hello, Dewi". Now right-click and choose View Source (the browser menu item that shows a page's code). It shows plain HTML, with Dewi's name already written in it. But the shop first had to find that name in a database (the place where a website stores its data). The code that found it does not appear anywhere. That code ran on the server (the computer that stores the website), before the page was sent to you. On many websites, the code on the server is written in PHP.
PHP is a programming language that runs on the server. Its main job is to build a page before that page is sent to the browser. Picture Mrs. Tini's Stall, a small food stall. The customer orders at the counter: that is the browser sending a request (asking for a page). Mrs. Tini cooks in the kitchen: that is PHP running on the server. Only a plate of finished food comes out of the kitchen: that is the HTML. The customer never sees the kitchen. The browser never sees the PHP code either.
PHP runs on the server
The table shows one visit to http://localhost:8000/stall.php. The word localhost means the server is your own computer.
| Step | Where | What happens |
|---|---|---|
| 1 | Browser | sends the request GET /stall.php (GET means: give me a page) |
| 2 | Server | finds the file stall.php and hands it to PHP |
| 3 | PHP | runs the code between <?php and ?>, the rest is sent as it is |
| 4 | Server | sends the result, plain HTML, as the response (the answer to the request) |
| 5 | Browser | draws that HTML |
Before you look at the example, make a guess. A PHP file holds <p>Total: <?php echo 15 * 2; ?></p>. In PHP, echo is the command that writes text. A visitor opens the page and clicks View Source. What do they see: <?php echo 15 * 2; ?>, 15 * 2, or 30? The answer comes after the example below.
This is stall.php, a mix of HTML and PHP:
<h1>Mrs. Tini's Stall</h1>
<?php
echo "<p>Today's menu: Fried Rice</p>";
echo "<p>Price: $15</p>";
?>
<p>Opens at 07:00</p>
And this is what the browser receives, exactly as View Source shows it:
<h1>Mrs. Tini's Stall</h1>
<p>Today's menu: Fried Rice</p><p>Price: $15</p><p>Opens at 07:00</p>
- Lines outside
<?php ... ?>are sent as they are. echowrites text into the result, in the exact place where the PHP code stands.echoadds no line break, and PHP drops a line break that comes right after?>. So the three<p>are on one line.- The PHP code itself is never sent.
Now the answer to the guess. The line <p>Total: <?php echo 15 * 2; ?></p> reaches the browser as <p>Total: 30</p>, because PHP already calculated 15 * 2 on the server.
That is why PHP is used for work that must happen on the server: reading data from a database, checking a password at login, saving an order.
The name PHP stands for PHP: Hypertext Preprocessor. The name contains itself: the first P also stands for PHP. The name means that PHP processes the page (hypertext) before the page is sent.
Your first PHP script
A script is a file that holds code. A script that holds only PHP is written like this:
<?php
// stall.php
echo "Welcome to Mrs. Tini's Stall!";
Run it in a terminal (a window where you type commands) with php stall.php:
$ php stall.php
Welcome to Mrs. Tini's Stall!
The $ sign marks the command you type, and you do not type the sign itself. The line below it is the result.
<?phpis the opening tag. Everything after it is read as PHP code.//starts a one-line comment: a note for people that does not run. A comment over several lines goes between/*and*/.echowrites text. The text goes between quotes.;(the semicolon) ends one statement (one command), like the full stop at the end of a sentence.- In a file that holds only PHP, the closing tag
?>may (and should) be left out.
print "Hello"; writes text too, with the same result. But echo is the one used most.
In BelajarWeb.dev's exercises, the Run button runs your code with a PHP simulator (a program that imitates PHP) in the browser. So you don't need to install anything yet. On your own computer, once PHP is installed, the command php -S localhost:8000 in the project folder starts a small server for learning. After that, you can open http://localhost:8000/stall.php in the browser.
Other options for running PHP on your computer:
| Way | Pick it when |
|---|---|
php -S localhost:8000 (used here) |
you are just starting. You need only PHP, no other program. |
| XAMPP or Laragon | you use Windows and want PHP, a web server and a MySQL or MariaDB database in one installer. Laragon is free for non-commercial use. |
| Laravel Herd | you use a Mac or Windows and want to learn Laravel later. Its free version has PHP and a web server, without MySQL. |
| Docker | the PHP version and its settings must match the real server exactly, for example in a team. Docker runs the server inside a separate container. |
When the semicolon is missing
In this file, the semicolon at the end of line 2 is missing:
<?php
echo "Hello World"
echo "Welcome";
PHP refuses this file with the message:
Parse error: syntax error, unexpected token "echo", expecting "," or ";" in /app/stall.php on line 3
- PHP reads the whole file first, before it runs anything. When the code has a writing mistake (a parse error), not a single line runs. HTML above the mistake is not sent either.
- The line number in the message points to where PHP noticed something was wrong, which is line 3. There PHP found
echowhile it was waiting for a comma or a semicolon. The missing semicolon belongs at the end of line 2. So always check the line before too. - A last statement left open, like
<?php echo "Hello World", ends withParse error: syntax error, unexpected end of file, expecting "," or ";". It means the file ended before PHP found a semicolon.
⚠️ Why isn't the PHP running?
- Forgetting
<?php. Without the opening tag, PHP treats the whole file as plain text and sends it as it is. As a result, visitors see your code, such asecho "...";, on the page. - Opening a
.phpfile straight from the folder. Double-clicking the file opens it without a server, so PHP never runs. Open it through the server's address, for examplehttp://localhost:8000/stall.php. - A quote left open. In
echo "Hello;PHP reads everything after it as text, up to the next quote or the end of the file. So the error appears below the line that is wrong, sometimes far below. - Old tutorials. Many examples online still use PHP 5 or 7, and some of that code no longer runs on newer versions. This track uses PHP 8.4. The newest version right now is PHP 8.5 (released 20 November 2025), and 8.4 still gets security fixes until 31 December 2028. Check the list at php.net/supported-versions.php.
TL;DR: PHP (PHP: Hypertext Preprocessor) is a programming language that runs on the server, the computer that stores a website. PHP runs the code between <?php and ?> and sends the result as HTML. So the PHP code never reaches the browser, and it does not appear in View Source (the browser menu item that shows a page's code). PHP code starts with <?php, echo (or print) writes text, and every statement ends with ; (a semicolon). A forgotten semicolon makes the whole file fail with a Parse error (a mistake in how the code is written). The line number in the message is often one line after the real mistake.
⚡ PHP runs on the server
Guess first: a PHP file holds <p>Total: <?php echo 15 * 2; ?></p>, and echo means "write this". What does a visitor see in View Source (the page's code as the browser received it)?
<p>Total: 30</p>. PHP already calculated 15 * 2 on the server, and the PHP code itself is never sent.
Meaning: PHP runs on the server (the computer that stores the website). There it runs the code between <?php and ?>, before the page is sent. The browser only receives the resulting HTML, like a customer at a food stall who sees the plate of food (the HTML) and never the kitchen (the PHP code).
⚡ Your first PHP script
Write:
<?php
// stall.php
echo "Welcome to Mrs. Tini's Stall!";
Meaning: <?php opens the PHP code, // starts a comment that does not run, and echo writes the text between the quotes. The semicolon ; ends the statement (one command). In a file that holds only PHP, leave out the closing tag ?>.
⚡ The forgotten semicolon
Guess first: a PHP file has two echo statements, on line 2 and line 3. The semicolon at the end of line 2 is missing. Which line does the error message point to?
Line 3. That is where PHP found the next echo while it was waiting for a semicolon. Also, not a single line runs, because PHP reads the whole file first, before it runs anything.
Meaning: Every statement ends with ; (a semicolon). When a Parse error (a mistake in how the code is written) appears, check the line named in the message and the line before it too.