What is PHP and where it runs

What is PHP

PHP is a server-side scripting language built for the web. A visitor requests a page, PHP code runs on the server, and the server sends back plain HTML.

Unlike scripts in the browser, PHP never runs on the visitor's computer in a normal setup. It runs where your site lives: on the server.

<?php $message = "Hello from the server!"; echo $message;

php message.php

Hello from the server!

The file message.php holds one assignment and one echo: the visitor's browser never receives the variable or the code, only the sentence that the echo printed.

Where PHP runs

The same language runs in two places. Behind a web server it builds pages and APIs. In the terminal it runs scripts, tests, and maintenance jobs.

On your own machine the built-in web server is enough for learning. You will meet it in the next lesson.

<?php echo PHP_SAPI; // cli or fpm-fcgi echo PHP_VERSION; // for example 8.5.0

php sapi.php

cli8.5.13

echo adds no line break of its own, so the two values run together: this machine answers cli for the command-line interface and 8.5.13 for its version, while a page served by PHP-FPM answers fpm-fcgi and the version of that installation.

Why learn PHP 8.5

Modern PHP is strict, fast, and typed. Each PHP 8 release added real types, faster execution, and cleaner syntax, and the full story lives in PHP versions.

Millions of sites run on PHP, from small blogs to large shops, so the first job or freelance order is never far away. Hosting, frameworks, and packages are everywhere.

<?php function total(float $price, int $qty): float { return $price * $qty; } var_dump(total(9.5, 4)); // float(38)

php total.php

float(38)

The result goes to var_dump(), which prints the type beside the value, so float(38) proves that the function returned a float and not the string "38". The parameter types and the return type are part of the function signature, and PHP refuses a call that breaks them.

Check your setup

You need two things for the next lesson: PHP 8.1 or newer and a terminal. Any code editor works, even a plain one without plugins.

<?php if (version_compare(PHP_VERSION, "8.1.0", ">=")) { echo "Ready for the course!"; }

php check.php

Ready for the course!

version_compare() reads two version strings and the operator it should test, so the same check works on 8.1.0 and on 8.5.13. A machine below 8.1 prints nothing, because the condition is false and the body of the if never runs.

Exercises

  1. Change the first program so that it prints the interface from PHP_SAPI too, on a line of its own. Which constant gives you that line break?
  2. Make total() print the price, the quantity and the result, each on its own line. What happens to the value the function returns?
  3. Replace "8.1.0" in the check program with "9.0.0", run it, and say what is printed and why.
  4. Write a script that prints Hello, world! with a single echo, check it with php -l, then run it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does PHP code run in a normal web setup?

    Answer

    On the server, before the HTML is sent to the visitor

    PHP is a server-side language: the server runs the script and sends only its output, which is usually HTML.

  2. What does a PHP script send to the visitor's browser?

    Answer

    The HTML that the code printed

    The browser has no PHP engine: it receives the markup and the text that the script produced.

  3. Which constant tells a script whether it runs under cli or under fpm-fcgi?

    Answer

    PHP_SAPI

    PHP_SAPI names the interface: cli in the terminal, fpm-fcgi behind a web server, and a few others such as apache2handler.

  4. Which function prints a value together with its type, as in float(38)?

    Answer

    var_dump()

    var_dump writes the type and the value; echo and print write the value alone.

  5. What does this program print? <?php echo "Hello, "; echo "world!";

    Answer

    Hello, world!

    echo adds no line break, so the second string continues the first on the same line.

  6. What does this program print? <?php $message = "Hello from the server!"; echo $message;

    Answer

    Hello from the server!

    echo prints the value the variable holds, not the name of the variable.

  7. What does this program print? <?php function total(float $price, int $qty): float { return $price * $qty; } var_dump(total(9.5, 4));

    Answer

    float(38)

    The two arguments multiply to 38, and the return type makes the result a float, which var_dump shows as float(38).

  8. What does this program print on PHP 8.5.13? <?php if (version_compare(PHP_VERSION, "8.1.0", ">=")) { echo "Ready for the course!"; }

    Answer

    Ready for the course!

    8.5.13 is greater than 8.1.0, so the comparison is true and the body of the if runs.

  9. Two echo statements print cli and 8.5.13, and the terminal shows cli8.5.13 on one line. Why?

    Answer

    echo adds no line break, so the second value follows the first

    echo prints exactly its arguments; a line break needs PHP_EOL or a newline inside the string.

  10. Which command runs the script message.php in the terminal?

    Answer

    php message.php

    The php binary takes the script path as its argument; the file itself is a data file for the interpreter.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev