What Is an Associative Array in PHP?
A PHP associative array stores values under named keys. Learn to create, read, loop, merge and sort them, and how keys are converted.
59 posts about PHP: questions with answers, tutorials and articles, newest first.
A PHP associative array stores values under named keys. Learn to create, read, loop, merge and sort them, and how keys are converted.
Create PHP arrays with square brackets, read elements by index, add and remove items and loop with foreach, with the core array functions.
include warns and continues, require stops the script. See the _once variants, return values, safe paths and when to use each statement.
Learn how PHP recursion works, with factorial, Fibonacci, array flattening and directory examples, plus the depth limits and loop alternatives.
PHP arrow functions are short single-expression closures written with fn. See the syntax, how they capture variables and when to use a closure instead.
Anonymous functions are unnamed functions that PHP stores as Closure objects. Learn use, arrow functions, binding and first-class callables.
Use the ... operator to accept any number of arguments in a PHP function and to unpack arrays into arguments, with typed examples for PHP 8.
declare(strict_types=1) makes PHP reject wrong scalar types with a TypeError instead of converting them. See how it works and where it applies.
Declare scalar parameter types and return types in PHP, from int and string to nullable, union, void and never, and see how PHP checks them.
Default values make parameters optional and named arguments let you pass them by name in any order. See the rules, errors and best practices in PHP 8.
PHP copies arguments by default, and & makes a parameter an alias of the caller's variable. See how objects, arrays and foreach behave with each mode.
Define a PHP function with the function keyword, pass arguments, return values and understand variable scope with short, runnable examples.
break ends a PHP loop and continue skips to the next round. See how levels like break 2 work in nested loops and inside switch.
Use foreach to loop over any PHP array: read values, get keys, modify elements by reference and unpack nested rows safely.
Learn when to use for, while and do while in PHP, how each loop checks its condition, and how to avoid infinite loops and off-by-one errors.
PHP 8 match is a strict expression that returns a value from the first matching arm. See how it differs from switch and when to prefer it.
The PHP switch statement runs the code of the first case that matches a value. Learn break, default, fall-through and the loose comparison trap.
PHP if, else and elseif run code based on conditions. See the syntax, truthiness rules and shorter alternatives such as match and ??.
isset, empty and is_null look similar but treat 0, empty strings and null differently. See a full comparison table and which to use when.
Superglobals are built-in PHP arrays available everywhere. See what each one contains and how to validate and escape the data safely.