Lesson 1 of 12

PHP Introduction

PHP is a programming language that runs on a web server and builds web pages before they reach the browser. It powers a large share of the web, including WordPress, and it is one of the friendliest ways to start writing back-end code. In this PHP introduction you will learn what PHP does, how a request flows through it, and you will write your first script.

What is PHP?

PHP stands for PHP: Hypertext Preprocessor (yes, the name refers to itself). It is a server-side scripting language. That means the code runs on the computer that hosts the website, not in the visitor’s browser. The browser only ever receives the result, which is usually HTML.

Compare this with JavaScript in the browser. In the JavaScript introduction you saw code that runs on the visitor’s device after the page loads. PHP works one step earlier: it decides what the page should contain in the first place. A visitor can never see your PHP source code with “View source”, only the HTML it produced.

What can PHP do?

  • Generate pages with content that changes, such as today’s date, a user’s name or a list of products.
  • Read data sent from HTML forms and validate it.
  • Talk to databases such as MySQL or MariaDB to store and fetch data (you will meet SQL in the SQL track).
  • Read and write files on the server, send emails and set cookies.
  • Control who can see which page, for example a login area.

Because it was designed for the web from day one, many of these jobs need only a few lines of PHP. That is why it is still a sensible first back-end language in 2026.

How a PHP page is served

It helps to picture the journey of a single request:

  1. The visitor types an address like example.com/about.php.
  2. The web server (Apache or Nginx, for example) sees the .php extension and hands the file to the PHP engine.
  3. PHP runs the code inside the file from top to bottom.
  4. Whatever the script prints becomes the response, usually plain HTML.
  5. The browser receives that HTML and displays it like any other page.

Every request starts fresh. Variables you create in one request are gone when the page has been sent. Later lessons show how forms, files and databases keep information between requests.

What you need to get started

To try the examples in this PHP introduction on your own computer, you need the PHP engine and, for web pages, a web server. Beginners on Windows often install a bundle such as XAMPP, which includes Apache, PHP and MariaDB in one package. On macOS or Linux you can install PHP with a package manager. Once PHP is installed, check the version from a terminal:

php -v

This track uses PHP 8.2 or newer. Older versions such as PHP 7 no longer receive security fixes, so always pick a current release, both locally and on your hosting plan. You also need a code editor; any editor with PHP syntax highlighting is fine.

Tip: PHP also includes a small built-in web server for testing. Open a terminal in your project folder and run php -S localhost:8000, then visit http://localhost:8000. Do not use it on a live site; it is meant for development only.

Your first PHP script

PHP code lives inside the tags <?php and ?>. Anything outside these tags is sent to the browser unchanged. The echo statement prints text. Save the following as hello.php and open it through your server:

<?php
echo "Namaste from PHP!";
Output
Namaste from PHP!

Notice there is no closing ?> tag. In a file that contains only PHP, leaving it out is recommended, because it prevents stray spaces or blank lines after the tag from being sent by accident.

Mixing PHP with HTML

The real power appears when PHP fills parts of an HTML page. Here the heading is plain HTML, but the paragraph text is created by PHP each time the page loads:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first PHP page</title>
</head>
<body>
  <h1>Welcome to Pondicherry Books</h1>
  <p>We have <?php echo 120 + 30; ?> books in stock.</p>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first PHP page</title>
</head>
<body>
  <h1>Welcome to Pondicherry Books</h1>
  <p>We have 150 books in stock.</p>
</body>
</html>

The browser receives only the HTML shown in the output. The <?php ... ?> part has already been replaced by its result. Here a closing tag is needed, because HTML follows the PHP code.

Watch out: Opening a .php file directly from your file manager (a file:// address) will not run the code. The browser just shows the raw source or downloads the file. Always open PHP pages through a web server, such as http://localhost/.

Running PHP from the command line

You can also run a script in a terminal without any browser: php hello.php. This is handy for quick experiments, and it is how the outputs in this track were produced. Everything in the upcoming lessons on variables, strings, loops and functions works the same way in the terminal and on a web page.

Next steps after this PHP introduction

Now that you know where PHP runs, the next lesson, PHP Syntax and Variables, covers the basic rules of the language: statements, semicolons, comments and how to store values in variables.

Further reading

Key takeaways

  • PHP is a server-side language: it runs on the web server and sends the result, usually HTML, to the browser.
  • Visitors never see your PHP source code, only its output.
  • PHP code goes between <?php and ?>; echo prints text.
  • Leave out the closing ?> in files that contain only PHP.
  • Use a current version (PHP 8.2 or newer) and open pages through a web server, not as local files.

Quick quiz

Check what you learned – pick an answer.

1. Where does PHP code run?

2. What does a visitor see if they choose "View source" on a PHP page?

3. Why is it recommended to omit the closing ?> tag in a file that contains only PHP?

Practice exercise

Create a file called about.php that shows an HTML page with an <h1> heading of your name and a paragraph whose text is printed by PHP with echo: "I am learning PHP in Chennai."

Show a hint

Write normal HTML, then put <?php echo "..."; ?> inside the paragraph.

Show the solution
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>About me</title>
</head>
<body>
  <h1>Kavya R</h1>
  <p><?php echo "I am learning PHP in Chennai."; ?></p>
</body>
</html>

Progress is saved in this browser only.