Skip to content
Educora
Beginner13 min1 / 10

What is PHP? Your first script

Learn how PHP works on the server, run a script in the terminal and in the built-in web server, and write your first program with echo and print.

Check yourself
In this lesson you will learn
  • Explain what PHP is and how it works on the server side
  • Run a script with the php file.php and php -S commands
  • Use echo, print, the <?= tag and comments

When you read an article on Wikipedia or open a news site built on WordPress, the page is prepared for you by a distant computer — a server. Very often that job is done by PHP: it takes data from a database, assembles an HTML page and sends it to your browser. In this lesson you will learn how PHP works and write your first script.

What is PHP?

PHP is a server-side scripting language. Rasmus Lerdorf created it in 1994 for his own personal website, and today it is developed by a large open community. The name first came from Personal Home Page; now it is a recursive acronym: PHP: Hypertext Preprocessor. Modern versions belong to the PHP 8.x series, and a new version usually comes out once a year, near the end of the year.

  • WordPress — more than 40% of all websites in the world run on this PHP system.
  • Laravel and Symfony — the most popular PHP frameworks for building web applications.
  • Wikipedia — its engine, MediaWiki, is written in PHP.
  • Online stores — platforms such as WooCommerce and Magento are also built with PHP.

How the server side works

  1. 1
    Request

    The browser sends a request to the server, for example it opens https://example.com/weather.php.

  2. 2
    PHP starts

    The web server (Apache or Nginx) sees the .php extension and hands the file over to PHP.

  3. 3
    The code runs

    PHP executes the code: it queries the database, does calculations and produces HTML text.

  4. 4
    Response

    The server sends the finished HTML back to the browser. The user never sees the PHP code — only its result.

Running PHP

You can download PHP from the official php.net website; on Windows, ready-made bundles such as XAMPP or Laragon are also handy, on macOS people use Homebrew, and on Linux the package manager. After installing, you work with these commands in the terminal:

CommandWhat it does
php -vshows the installed PHP version
php hello.phpruns a script in the terminal (CLI mode)
php -l hello.phpchecks the syntax without running the code
php -S localhost:8000starts the built-in web server in the current folder
php -aopens interactive mode for trying code line by line
The main terminal commands for working with PHP

Your first script

Create a file called hello.php, type the code below and run php hello.php in the terminal. PHP code always starts with the <?php opening tag, and every statement ends with a semicolon ;.

PHP
<?php

echo "Hello, Educora!\n";
print "PHP runs on the server.\n";
echo "Two plus three is ", 2 + 3, "\n";
Expected output
Hello, Educora!
PHP runs on the server.
Two plus three is 5
The file hello.php
  • echo prints text and can take several values separated by commas.
  • print takes only one value and always returns 1; in practice echo is used far more often.
  • "\n" is the newline character. echo does not move to a new line by itself, so you add it yourself.
  • In a file that contains only PHP code, the closing ?> tag is left out — this is safer, because no stray whitespace can slip in at the end of the file.

PHP inside HTML

PHP was designed from the start to be embedded in HTML. Everything outside the <?php ... ?> tags is sent as it is, while the code inside the tags is executed. The short <?= ... ?> tag writes the value of an expression straight into the page.

PHP
<?php $city = "Baku"; $temp = 24; ?>
<!DOCTYPE html>
<html>
<body>
    <h1>Weather in <?= $city ?></h1>
    <p>Temperature: <?= $temp ?> °C</p>
    <p>Built by PHP on the server</p>
</body>
</html>
Expected output
<!DOCTYPE html>
<html>
<body>
    <h1>Weather in Baku</h1>
    <p>Temperature: 24 °C</p>
    <p>Built by PHP on the server</p>
</body>
</html>
weather.php: only pure HTML is left in the result

To see this page in a browser, run php -S localhost:8000 in the file's folder and open http://localhost:8000/weather.php. The browser receives the HTML shown above; the variables $city and $temp have already been replaced with their values.

Comments and syntax rules

PHP has three kinds of comments: // and # turn the rest of the line into a comment, and /* ... */ covers several lines. Variable names always start with $ and are case-sensitive: $name and $Name are different variables. Keywords and function names, however, are not case-sensitive.

PHP
<?php

// a one-line comment
# also a one-line comment
/* a comment
   on several lines */
$name = "Leyla";
$Name = "Murad";
echo $name, " and ", $Name, "\n";
ECHO "Keywords are case-insensitive\n";
Expected output
Leyla and Murad
Keywords are case-insensitive
1 / 6

Key points

  • PHP is a server-side scripting language: its code runs on the server, and only the result (HTML) is sent to the browser.
  • WordPress, Laravel, Symfony and Wikipedia are built on PHP; modern versions belong to the PHP 8.x series.
  • php file.php runs a script in the terminal, and php -S localhost:8000 starts the built-in web server.
  • Code starts with <?php and every statement ends with ;; echo and print output text, and <?= writes a value into HTML.
  • Variable names start with $ and are case-sensitive; keywords are not.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Where is PHP code executed?