PHP Syntax References

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.

Most Useful PHP Syntax References

PHPSyntax

Print Output

Code example
echo "Hello";

Outputs text.

Learn More
PHPSyntax

Declare a Variable

Code example
$name = "Ada";

Variables start with $.

Learn More
PHPSyntax

Open a PHP Block

Code example
<?php

Starts PHP code in a file.

Learn More
PHPSyntax

If / Elseif / Else

Code example
if ($x > 0) { ... } elseif ($x == 0) { ... } else { ... }

Runs code depending on a condition.

Learn More
PHPSyntax

Foreach Loop

Code example
foreach ($items as $key => $value) { ... }

Loops over an array with keys and values.

Learn More
PHPSyntax

Create an Array

Code example
$nums = [1, 2, 3]; $nums[] = 4;

Creates an indexed array.

Learn More
PHPSyntax

Associative Array

Code example
$user = ['name' => 'Ada', 'age' => 36];

Creates key-value pairs.

Learn More
PHPSyntax

Define a Function

Code example
function add(int $a, int $b): int { return $a + $b; }

Defines a typed function.

Learn More

More PHP Syntax References

Frequently Asked Questions

What does this PHP syntax reference cover?

It covers 20 common PHP patterns and examples, including Print Output, Declare a Variable, Open a PHP Block, If / Elseif / Else, Foreach Loop.

Can I copy the PHP examples?

Yes. Each reference page includes a copyable code example.

What affects these PHP examples?

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.