PHP is a programming language designed for building websites. Its code runs on the server, the computer that hosts the website, not in the visitor's browser.
Here's what happens when someone opens a PHP page: the browser asks the server for products.php, the server runs the PHP code inside it, the code produces HTML (perhaps filling in today's prices from a database), and only that finished HTML is sent back. Visitors never see your PHP code, only its result.
In this lesson you'll install PHP, run a local development server, write your first script with echo, and mix PHP into an HTML page.
Installing and running PHP
- Install PHP from php.net/downloads. On Windows, bundles such as XAMPP or Laragon install PHP for you. On macOS,
brew install phpworks if you use Homebrew. - Check it in a terminal:
php --version- Create a folder with a file called
index.php, open a terminal in that folder and start PHP's built-in server:
php -S localhost:8000Now open http://localhost:8000 in your browser to see the page. Leave the terminal running while you work, and refresh the browser to see changes.
PHP tags and echo
PHP code goes between an opening <?php tag and a closing ?> tag. Everything outside the tags is sent to the browser unchanged.
<?php
echo "Hello from PHP!";
?>echooutputs text. You can echo several values separated by commas.- Every statement ends with a semicolon.
- In a file that contains only PHP, it's normal to leave out the closing
?>.
You can also run a script in the terminal with php hello.php, which is handy for quick tests.
Mixing PHP and HTML
The real power of PHP is dropping small pieces of code into an HTML page:
<h1>Today's special</h1>
<p>Soup of the day: <?php echo "Tomato"; ?></p>
<p>Page built at <?= date("H:i") ?></p><?= ... ?> is a short form of <?php echo ... ?>, perfect for printing one value inside HTML. The browser receives plain HTML, with the PHP parts already replaced by their output.
Comments
PHP ignores comments:
// a single-line comment
# also a single-line comment
/* a comment over
several lines */Use comments to explain why the code does something. HTML comments <!-- --> are different: they're sent to the browser, so never put anything private in them.
What happens during a PHP request
Every time a browser asks for a PHP page, the same steps run:
- The browser sends an HTTP request for a URL, such as
/products.php?category=bags. - The web server, such as Nginx or Apache, passes the request to PHP. In production this is usually PHP-FPM, a service that keeps PHP processes ready to work.
- PHP runs the script from the top. It can read the URL and form data, query a database and build HTML.
- The finished output is sent back as the response, and PHP throws away the script's variables.
That last step matters: each request starts with a clean slate. A variable set while one visitor loads a page doesn't exist when the next page loads. Anything that must survive between requests, such as a logged-in user or a shopping basket, goes into a session, a cookie, a file or a database.
PHP versions
PHP 8 introduced major improvements, including match, named arguments, the nullsafe operator ?->, enums and much better performance. Each PHP 8 release receives active support for two years and security fixes for two more. Older versions such as PHP 7 no longer receive security updates, so new projects should use a currently supported PHP 8 release. Run php --version to see which version you have; the examples in this course need PHP 8.1 or newer.
PHP Syntax
PHP tags and output
<?php
// PHP code here
echo "text";
echo "a", "b", "c";
?>
<p><?= $value ?></p>PHP Summary
- PHP runs on the server and sends its output, usually HTML, to the browser.
- Start a local server with
php -S localhost:8000and open it in a browser. - Code goes between
<?phpand?>; statements end with;. echooutputs text;<?= value ?>prints a value inside HTML.- Comments use
//,#or/* */.
PHP Examples
Each example shows the code, the output it produces and what each part does.
Hello from PHP
<?php
echo "Hello, world!";
echo "<br>";
echo "PHP version: ", PHP_MAJOR_VERSION;Hello, world!<br>PHP version: 8Line-by-line explanation
- Line 2echo sends text to the browser.
- Line 3You can echo HTML too. The browser turns <br> into a line break.
- Line 4Several values separated by commas. PHP_MAJOR_VERSION is a built-in constant.
A page with PHP inside
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the bakery</h1>
<p>Fresh today: <?= "Cinnamon buns" ?></p>
<p>Open until <?php echo 18; ?>:00</p>
</body>
</html><!DOCTYPE html>
<html>
<body>
<h1>Welcome to the bakery</h1>
<p>Fresh today: Cinnamon buns</p>
<p>Open until 18:00</p>
</body>
</html>Line-by-line explanation
- Line 4Plain HTML is sent exactly as written.
- Line 5The short echo tag prints one value.
- Line 6A full PHP block works inside HTML too. The output shows the HTML the browser receives, with no PHP left in it.
Maths and comments
<?php
// Work out a simple bill
echo 3 * 4.50, "\n";
echo 10 / 4, "\n";
echo intdiv(10, 4), "\n";
echo 10 % 4, "\n";13.5
2.5
2
2Line-by-line explanation
- Line 2A comment. PHP ignores it.
- Line 3"\n" is a line break in the terminal. In a web page you'd use <br>.
- Line 4/ gives a decimal result when needed.
- Lines 5 to 6intdiv() gives the whole-number part; % gives the remainder.
Common PHP Mistakes
Missing semicolon
<?php
echo "Hi"
echo "there";<?php
echo "Hi";
echo "there";PHP stops with "syntax error, unexpected token echo".
Opening the file directly
# double-clicking index.php
file:///C:/site/index.phpphp -S localhost:8000
# then open http://localhost:8000/index.phpForgetting the opening tag
echo "Hello";<?php
echo "Hello";Without <?php, PHP treats the line as plain text and sends it to the browser as it is.
PHP in Real Projects
PHP on the web
When you load a WordPress blog, PHP fetches the latest posts from a database and wraps them in the site's HTML theme. Online shops use PHP to show stock levels and prices, and contact forms use it to send emails. In every case, the browser just receives ordinary HTML.
PHP FAQ
Is PHP still used in 2026?
Yes. PHP powers WordPress, which runs a large share of all websites, along with platforms such as Drupal, Magento and MediaWiki, the software behind Wikipedia. Modern frameworks such as Laravel and Symfony are widely used to build new web applications and APIs.
What is the difference between PHP and JavaScript?
PHP runs on the server and sends finished HTML or JSON to the browser. JavaScript in a web page runs inside the visitor's browser and can change the page after it has loaded. Many sites use both: PHP to build pages and handle data, JavaScript for interactive features. JavaScript can also run on servers through Node.js.
Do I need Apache or another web server to learn PHP?
Not at first. PHP's built-in server, started with php -S localhost:8000, is perfect for learning and development. Real websites run behind a production web server such as Nginx or Apache, usually with PHP-FPM; never use the built-in server for a public site.
Can PHP run outside a web browser?
Yes. The command php script.php runs a script in the terminal, which is how PHP is used for scheduled jobs, data imports, command-line tools and tests. Composer and PHPUnit are themselves PHP programs that run on the command line.
Is PHP free?
Yes. PHP is open source and free for any use, including commercial websites. Most hosting companies include PHP in their plans, which is one reason it became so widespread for websites.
PHP Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the script so it outputs "Hi".
"Hi";
Where does PHP code run?
What HTML does the browser receive?
<p>Total: <?= 2 + 3 ?></p>
Which command starts PHP's built-in development server?
Which command starts PHP's built-in web server on port 8000?
PHP Quiz
5 questions to check what you learned.
Which tag starts a block of PHP code?
- <?php
- <php>
- <script php>
- {{php
What does
<?= $name ?>do?- Declares a variable
- Compares $name
- Comments out $name
- Prints the value of $name
Can visitors see your PHP code with "View source"?
- Yes, always
- Only in Chrome
- No, they only see the HTML that PHP produced
- Only if the code has comments
Why might a PHP page show raw code instead of running?
- PHP is broken
- It was opened as a file instead of through a server
- The file name is too long
- It's missing a CSS file
Which tag starts a block of PHP code?
- <?php
- <php>
- <script php>
- {php}