- Explain what PHP is and how it works on the server side
- Run a script with the
php file.phpandphp -Scommands - 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
- 1Request
The browser sends a request to the server, for example it opens
https://example.com/weather.php. - 2PHP starts
The web server (Apache or Nginx) sees the
.phpextension and hands the file over to PHP. - 3The code runs
PHP executes the code: it queries the database, does calculations and produces HTML text.
- 4Response
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:
| Command | What it does |
|---|---|
php -v | shows the installed PHP version |
php hello.php | runs a script in the terminal (CLI mode) |
php -l hello.php | checks the syntax without running the code |
php -S localhost:8000 | starts the built-in web server in the current folder |
php -a | opens interactive mode for trying code line by line |
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
echo "Hello, Educora!\n";
print "PHP runs on the server.\n";
echo "Two plus three is ", 2 + 3, "\n";Hello, Educora! PHP runs on the server. Two plus three is 5
echoprints text and can take several values separated by commas.printtakes only one value and always returns1; in practiceechois used far more often."\n"is the newline character.echodoes 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 $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><!DOCTYPE html>
<html>
<body>
<h1>Weather in Baku</h1>
<p>Temperature: 24 °C</p>
<p>Built by PHP on the server</p>
</body>
</html>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
// 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";Leyla and Murad Keywords are case-insensitive
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.phpruns a script in the terminal, andphp -S localhost:8000starts the built-in web server.- Code starts with
<?phpand every statement ends with;;echoandprintoutput 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.