PHP (short for Hypertext Preprocessor) is a powerful and widely-used open-source programming language that executes on the server. It is an essential tool for creating dynamic and interactive websites. PHP primarily runs on a web server (e.g., Apache), where it reads the source code and generates an HTML page as a result, which is then seen by the user. It works on all major platforms, such as Windows, Linux, and macOS, and supports a wide range of databases. With PHP, you can create everything – from simple news portals to complex e-commerce stores and social networks.
switch statements.
// Example of using the modern "match" expression in PHP 8
$status_code = 404;
$response = match($status_code) {
200 => 'Everything is fine (OK)',
404 => 'Page not found (Not Found)',
500 => 'Server error (Internal Server Error)',
default => 'Unknown status',
};
echo $response; // Outputs: Page not found (Not Found)
Modern PHP is based on objects. Classes allow us to organize code into logical units that can be reused multiple times:
class Car {
public $brand;
public $model;
// The constructor is called when the object is created
public function __construct($brand, $model) {
$this->brand = $brand;
$this->model = $model;
}
// Method to output the description
public function printDescription() {
return 'This is a vehicle of the brand ' . $this->brand . ', model ' . $this->model . '.';
}
}
// We create a new object (an instance of the class)
$myCar = new Car('Audi', 'A4');
echo $myCar->printDescription();
A quick overview of the most common statements, their differences, and basic data types in PHP:
| Name | Description | Example |
|---|---|---|
include |
Includes an external file. If the file is missing, it throws a Warning and continues execution. | include "header.php"; |
include_once |
Includes a file only once. If already included, it ignores it. Continues on error. | include_once "functions.php"; |
require |
Strictly includes an external file. If missing, it triggers a Fatal Error and stops the script. | require "config.php"; |
require_once |
Strictly includes a file only once. Immediately stops the entire script on error. | require_once "database.php"; |
echo |
Faster statement used to output one or more text strings to the screen. | echo "Hello!"; |
print |
Statement used to output a single string. Always returns 1, making it slightly slower than echo. | print "Text"; |
| Name | Description | Example |
|---|---|---|
String |
Text or a sequence of characters written inside quotes. | ▶ View Code |
Integer |
A whole number without decimal places (positive or negative). | ▶ View Code |
Float |
A decimal number where a dot is used instead of a comma. | ▶ View Code |
Boolean |
A logical value that can only be true or false. | ▶ View Code |
Array |
An array or a structure that can store multiple values at once. | ▶ View Code |
Object |
An instance of a class, combining data (properties) and functions (methods). | ▶ View Code |
Null |
A special data type that represents a variable with no value. | ▶ View Code |
Resource |
A special variable holding a reference to an external resource (e.g., an open file or database). | ▶ View Code |