contentintech
Languages

PHP

Arrays, functions and safe database query patterns.

Updated Official documentation ↗

CLI and values

php -vInspect the interpreter version
php -l index.phpCheck syntax without running the script
$name = 'Ada';Assign a variable
$count = 3;Store an integer

Arrays and functions

$names = ['Ada', 'Lin'];Create an indexed array
$user = ['name' => 'Ada', 'id' => 1];Create a key-value array
function add(int $a, int $b): int { return $a + $b; }Define parameter and return types
array_map(fn($n) => $n * 2, [1, 2, 3]);Transform array values

Safer application code

$name = $input['name'] ?? 'Guest';Default an absent or null value
htmlspecialchars($name, ENT_QUOTES, 'UTF-8');Escape untrusted text for HTML output
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');Prepare a query rather than concatenate input
$stmt->execute([$id]);Bind query values through PDO

Section navigation