⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

PHP Tutorial

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.

EXAMPLE
RESULT
The result will be displayed here...

What can PHP do?

  1. Generates dynamic page content in real-time.
  2. Manages files on the server (creating, reading, writing, deleting).
  3. Securely collects and processes data from web forms.
  4. Manages cookies and sessions for user logins.
  5. Adds, deletes, and modifies data in databases (e.g., MySQL).
  6. Controls user access to different parts of the website.
  7. Encrypts sensitive data for enhanced security.

Where is PHP most commonly used?

  1. CMS systems: The largest systems, such as WordPress, Joomla, and Drupal, are built on PHP.
  2. Online stores: Platforms like Magento and WooCommerce enable sales through PHP.
  3. API development: PHP is an excellent choice for creating Restful APIs for mobile applications.
  4. Email services: Sending automated messages and notifications to users.
  5. Custom applications: Complete freedom in developing unique, tailor-made web solutions.

Main advantages of PHP

  1. Huge ecosystem: Thousands of libraries and frameworks (Laravel, Symfony) speed up development.
  2. Proven stability: Giants like Facebook and Wikipedia trust PHP.
  3. Great connectivity: Support for almost all databases (MySQL, PostgreSQL, SQLite).
  4. Simplicity: The language is beginner-friendly, yet provides all the tools for professionals.
  5. Free to use: PHP is open-source, meaning anyone can use it without license costs.

Popular PHP Frameworks

  1. Laravel: Currently the most popular framework with an elegant syntax and rich functionality.
  2. Symfony: A highly robust and flexible framework for complex enterprise systems.
  3. CodeIgniter: An exceptionally lightweight and fast framework, ideal for smaller projects.
  4. Phalcon: A framework that achieves top speeds because it is written as a C-extension.

Modern features (PHP 8+)

  1. JIT compiler: Significantly speeds up the execution of demanding mathematical and logical operations.
  2. Named Arguments: Arguments are passed to functions by name, making the code more readable.
  3. Match Expression: A smarter and shorter alternative to switch statements.
  4. Attributes: A modern way of adding metadata directly into class code.
  5. Null-safe operator: Prevents errors when accessing values that might not exist.
// 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)

Object-Oriented Programming (OOP)

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();

PHP Quick Overview

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