← Вернуться к курсу: PHP from scratch

What PHP is, installing it and the built-in server

Видео скоро появится

PHP is a programming language designed for the web. Its key feature is that the code runs on the server: the browser asks for a page, the server runs your PHP program, and only the result travels back, usually HTML. The visitor never sees your PHP code, only what that code produced. This is the opposite of JavaScript in the browser, which runs on the visitor's own computer.

Today PHP powers a huge part of the internet: WordPress, Laravel, Symfony and thousands of shops and admin panels. In this course we use PHP 8.3, a modern version with types, match, enums and readonly properties. If you read very old tutorials (PHP 5 or 7), many things will differ; here you will learn the current way of writing PHP.

Installing PHP and checking the version

You need the PHP command-line interpreter, with no Apache or anything else. On Windows you can download it from php.net (the Thread Safe or Non Thread Safe zip, it does not matter for learning) or install it with a package manager; on macOS use Homebrew (brew install php); on Ubuntu or Debian use apt (sudo apt install php-cli php-sqlite3). When you finish, open a terminal and check that it works. You should see version 8.3 or higher.

bash
php -v
# PHP 8.3.x (cli) ...

# list the extensions you have (look for pdo_sqlite later)
php -m

Your first program

Create a folder called php-course and inside it a file index.php. A PHP file can mix HTML and code; the code goes between the opening tag and, optionally, the closing tag. Everything outside those tags is sent as is. Each statement ends with a semicolon, and echo is the statement that writes text to the output.

php
<?php

echo 'Hello, PHP!';
echo "\n";
echo 'PHP version: ' . PHP_VERSION . "\n";

Run it with php index.php. The dot (.) joins texts. Notice that in the second echo we use double quotes so that \n becomes a line break; with single quotes, \n would be shown literally. A useful rule: in files that contain only PHP, do not write the closing tag, which avoids stray spaces that later break headers.

The built-in server: php -S

PHP includes a development web server. From the project folder run the command below and open http://localhost:8000 in the browser. Every time you reload, PHP runs the file again, so you do not need to restart anything after editing. Press Ctrl+C to stop it. It is for development only: in production you use Nginx or Apache with PHP-FPM.

bash
cd php-course
php -S localhost:8000
# [Sat Nov 22 10:00:00 2026] PHP 8.3.x Development Server (http://localhost:8000) started
php
<?php
$name = 'World';
?>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>My first page</title></head>
<body>
  <h1>Hello, <?= $name ?>!</h1>
  <p>The server time is <?= date('H:i:s') ?>.</p>
</body>
</html>

Notice the short form <?= ... ?>, which equals echo. It is very handy inside HTML. Each reload shows a different time, proof that the code runs on the server for every request. Common mistakes: forgetting the semicolon (you get a Parse error), saving the file with an .html extension (the server would show the code as text) or opening the file by double-clicking instead of using localhost.

✎ Мои заметки

Сохранено только в этом браузере. Войдите, чтобы хранить в аккаунте.0/5000 символов

Далее →