--- language: php author: Malcolm Fell author_url: http://emarref.net/ --- This document describes PHP 5+. ## [Basic Syntax](http://www.php.net/manual/en/language.basic-syntax.php) All statements must end with a semi-colon; All PHP code must be between tags. PHP can also be configured to respect the [short open tags](http://www.php.net/manual/en/ini.core.php#ini.short-open-tag) . ## [Comments](http://www.php.net/manual/en/language.basic-syntax.comments.php) ```php '$String' // Avoid using double quotes except to embed other variables $dbl_quotes = "This is a $sgl_quotes." // => 'This is a $String' // Escape special characters with backslash $escaped = "This contains a \t tab character."; // Enclose a variable in curly braces if needed $money = "I have $${integer} in the bank." // Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners $nowdoc = <<<'END' Multi line string END; $heredoc = << 1, "Two" => 2, "Three" => 3]; $associative["One"]; // Holds the value 1 ``` ## Output ```php ``` ## [Operators](http://www.php.net/manual/en/language.operators.php) ### Assignment ```php $b // TRUE if $a is not equal to $b after type juggling. $a < $b // TRUE if $a is strictly less than $b. $a > $b // TRUE if $a is strictly greater than $b. $a <= $b // TRUE if $a is less than or equal to $b. $a >= $b // TRUE if $a is greater than or equal to $b. // The following will only be true if the values match and are the same type. $a === $b // TRUE if $a is equal to $b, and they are of the same type. $a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. 1 == '1' // TRUE 1 === '1' // FALSE ``` ## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php) Variables can be converted between types, depending on their usage. ```php This is displayed if the test is truthy. This is displayed otherwise. ``` ### Switch statements ```php 2, "car" => 4]; foreach ($wheels as $vehicle => $wheel_count) { echo "A $vehicle has $wheel_count wheels"; } // This loop will stop after outputting 2 $i = 0; while ($i < 5) { if ($i == 3) { break; // Exit out of the while loop and continue. } echo $i++; } // This loop will output everything except 3 $i = 0; while ($i < 5) { if ($i == 3) { continue; // Skip this iteration of the loop } echo $i++; } ``` ## Functions Functions are created with the ```function``` keyword. ```php property; // Access to properties MyClass::myStaticMethod(); // myStaticMethod cannot be run on $cls ``` PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic.php) for classes. ```php $key; } public function __set($key, $value) { $this->$key = $value; } } $x = new MyClass(); echo $x->property; // Will use the __get() method $x->property = 'Something'; // Will use the __set() method ``` Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword. ```php myTraitMethod(); ``` ## More Information Visit the [official PHP documentation](http://www.php.net/manual/) for reference and community input. If you're interested in up-to-date best practices, visit [PHP The Right Way](http://www.phptherightway.com/). If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/).