From d9a67645f7a304c4287156ef30555ce984dbee47 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 09:49:03 -0700 Subject: Fix up PHP --- php.html.markdown | 197 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 135 insertions(+), 62 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 947ee4a7..cddba644 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -8,11 +8,14 @@ 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) . +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 = << ``` @@ -108,21 +133,27 @@ echo function_result(); // Output the result of a function call that returns a v ### Assignment ```php + $b // TRUE if $a is not equal to $b after type juggling. $a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. -$a < $b // TRUE if $a is strictly less than $b. +$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. @@ -133,14 +164,18 @@ $a >= $b // TRUE if $a is greater than or equal to $b. Variables can be converted between types, depending on their usage. ```php + @@ -192,16 +231,18 @@ if (/* test */) { ### Switch statements ```php + 2, "car" => 4]; foreach ($wheels as $vehicle => $wheel_count) { - echo "A $vehicle has $wheel_count wheels"; + 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. + break; // Exit out of the while loop and continue. } - echo $i++; + echo $i++; } // This loop will output everything except 3 $i = 0; while ($i < 5) { - if ($i == 3) { - continue; // Skip this iteration of the loop + if ($i == 3) { + continue; // Skip this iteration of the loop } - echo $i++; + echo $i++; } ``` @@ -255,8 +298,10 @@ while ($i < 5) { Functions are created with the ```function``` keyword. ```php +$key; + return $this->$key; } public function __set($key, $value) { - $this->$key = $value; + $this->$key = $value; } } $x = new MyClass(); -echo $x->property; // Will use the __get() method to retrieve the value of $property -$x->property = 'Something'; // Will use the __set() method to set the value of property +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 +