PHP syntax basics: variables, arrays, functions
A quick tour of typed variables, modern arrays, and arrow functions in PHP 8.5.
Table of Contents
Modern PHP syntax is compact and predictable. This part covers the three things you’ll type most: variables, arrays, and functions.
Variables and types
Variables start with $, and you can trust the types:
declare(strict_types=1);
$name = 'updeta';
$parts = 2;
Arrays
Arrays are ordered maps, and match beats switch for simple branching:
$level = 'beginner';
$label = match ($level) {
'beginner' => 'Start here',
default => 'Keep going',
};
Functions
Arrow functions capture variables automatically:
$prices = [10, 25, 40];
$withTax = array_map(fn ($p) => $p * 1.11, $prices);
That’s the daily syntax. Next series we build a small dynamic page with forms and validation.