Himanshu SharmaSoftware engineer
Let’s talk

PHP · Lesson 3 of 54

Your first PHP script

Write a first PHP script, run it from the command line, then serve it as a web page with PHP's built-in development server and see the HTML it sends.

12 minGetting startedUpdated 22 October 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonRun a PHP script both from the terminal and as a web page with the built-in server.

Create a folder and a file

Make a new folder for the course, for example php-course, and open it in your editor. Create a file called hello.php with this content:

php
<?php
echo "Hello from PHP!\n";

That is a complete PHP program. Line 1 is the opening tag, <?php. It tells PHP that what follows is code. Line 2 uses echo, which prints text, and ends with a semicolon, which ends the statement. The \n inside the double quotes is a line break, so the terminal prompt appears on a new line afterwards.

Run it in the terminal

Open a terminal in the same folder and run the file with the php command. The output appears straight away:

bash
php hello.php
result
Hello from PHP!

Running scripts like this is the quickest way to try things, and it is how most examples in this course show their output. Edit the text, save, run it again, and you see the change.

A script that does a little more

Programs become interesting when they calculate something. This version stores two prices in variables (names that start with $, covered properly in the next section), adds them and prints the total:

php
<?php
$keyboard = 89.50;
$mug = 18.75;
$total = $keyboard + $mug;

echo "Keyboard: $keyboard\n";
echo "Mug: $mug\n";
echo "Total: $total\n";
result
Keyboard: 89.5
Mug: 18.75
Total: 108.25

Notice that PHP printed 89.5, not 89.50. A number does not remember how it was typed. Formatting money with two decimals is done with number_format() or printf(), which you will meet in echo, print and printf.

Run it as a web page

PHP includes a small development web server, so you do not need Apache to see your pages in a browser. Create index.php in the same folder:

php
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first PHP page</title>
</head>
<body>
  <h1>Hello from PHP</h1>
  <p>This page was built on <?php echo date('Y'); ?>'s server, at request time.</p>
  <p>2 + 2 = <?php echo 2 + 2; ?></p>
</body>
</html>

Then start the server from the folder:

bash
php -S localhost:8000

Open http://localhost:8000/ in your browser. The server finds index.php, runs it, and sends the result. If you choose View Source in the browser, you see what was actually sent. Here is the real request and response:

http
GET /index.php
result
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first PHP page</title>
</head>
<body>
  <h1>Hello from PHP</h1>
  <p>This page was built on 2026's server, at request time.</p>
  <p>2 + 2 = 4</p>
</body>
</html>

The PHP tags are gone. Each <?php ... ?> block was replaced by what it printed, and the HTML around it was passed through unchanged. Press Ctrl+C in the terminal to stop the server.

The built-in server is for development only. Real websites run PHP behind a server such as Apache or Nginx, which shared hosting sets up for you.

The closing tag

In index.php, each PHP block has an opening <?php and a closing ?>, because the file switches between HTML and PHP. In a file that contains only PHP, such as hello.php, the convention is to leave the closing tag out. Anything after a closing tag, even one invisible line break, is sent as output. In a web application that stray output can arrive before the headers and cause the error "headers already sent". Leaving the tag out makes that impossible.

Checking syntax without running

php -l (lint) checks a file for syntax errors without running it. It is useful before uploading a file to a server:

bash
php -l hello.php

It prints No syntax errors detected in hello.php, or the line where PHP got confused.

Common mistakes

  • Saving the file as hello.php.txt. Some editors and Windows Explorer hide extensions. Make sure the name ends in .php.
  • Forgetting the semicolon at the end of a statement. PHP reports a "syntax error, unexpected" message pointing at the next line.
  • Opening the file with file:// in the browser instead of going through the server at http://localhost:8000. The browser cannot run PHP.
  • Running php -S in the wrong folder. The server serves the folder it was started in. Start it where your files are.

Exercise

Create about.php that prints a page with your name in an h1 and the sum of 19 and 23 in a paragraph, using one PHP block for the sum. Run it with the built-in server and check the page source.

Show one possible answer
php
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>About me</title></head>
<body>
  <h1>Asha Verma</h1>
  <p>19 + 23 = <?php echo 19 + 23; ?></p>
</body>
</html>
http
GET /about.php
result
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>About me</title></head>
<body>
  <h1>Asha Verma</h1>
  <p>19 + 23 = 42</p>
</body>
</html>

Key points

  • PHP code starts with the opening tag <?php. In a file that contains only PHP, leave out the closing ?> tag.
  • php hello.php runs a script in the terminal and prints its output there.
  • php -S localhost:8000 starts a development web server that runs .php files in the current folder.
  • echo prints text. In a web page that text becomes part of the HTML the browser receives.

Check yourself

1. Which tag starts a block of PHP code?
2. What does php -S localhost:8000 do?
3. Why do style guides say to leave out the closing ?> tag in a file that contains only PHP?
4. Is PHP's built-in server suitable for a production website?