Learn What is PHP?

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…

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>

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.

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

⚠️ Why isn't the PHP running?

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.

What you will learn