Print Output
echo "Hello";Outputs text.
PHP syntax quick reference: variables, arrays, functions, classes and JSON, from the official PHP manual. Every item below is clearly marked as a shortcut, command, or syntax reference.
echo "Hello";Outputs text.
$name = "Ada";Variables start with $.
<?phpStarts PHP code in a file.
if ($x > 0) {
...
} elseif ($x == 0) {
...
} else {
...
}Runs code depending on a condition.
foreach ($items as $key => $value) { ... }Loops over an array with keys and values.
$nums = [1, 2, 3];
$nums[] = 4;Creates an indexed array.
$user = ['name' => 'Ada', 'age' => 36];Creates key-value pairs.
function add(int $a, int $b): int {
return $a + $b;
}Defines a typed function.
Outputs text.
echo "Hello";Variables start with $.
$name = "Ada";Starts PHP code in a file.
<?phpRuns code depending on a condition.
if ($x > 0) {
...
} elseif ($x == 0) {
...
} else {
...
}Loops over an array with keys and values.
foreach ($items as $key => $value) { ... }Creates an indexed array.
$nums = [1, 2, 3];
$nums[] = 4;Creates key-value pairs.
$user = ['name' => 'Ada', 'age' => 36];Defines a typed function.
function add(int $a, int $b): int {
return $a + $b;
}Inserts variables into double-quoted strings.
echo "Hi {$name}";Joins strings with a dot.
$full = $first . " " . $last;Loops a set number of times.
for ($i = 0; $i < 10; $i++) { ... }Defines a class with constructor promotion.
class Dog {
public function __construct(public string $name) {}
}Uses a default if a value is null or unset.
$page = $_GET['page'] ?? 1;Loads another PHP file once.
require_once 'config.php';Converts between arrays and JSON.
json_encode($data);
json_decode($json, true);Counts array elements.
count($nums)Writes a short anonymous function.
$double = fn($x) => $x * 2;Handles exceptions.
try {
...
} catch (Exception $e) {
echo $e->getMessage();
}Dumps a variable's type and value.
var_dump($x);Returns a value based on strict matches.
$label = match ($n) {
1 => 'one',
default => 'other',
};It covers 20 common PHP patterns and examples, including Print Output, Declare a Variable, Open a PHP Block, If / Elseif / Else, Foreach Loop.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.