From 0ab2ec2d737dab34f5c962b53e49f96b74b829da Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:48:31 +1200 Subject: [PATCH 01/12] Fix spelling error concatinated -> concatenated --- php.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index 18324736..4dafe6ad 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -43,8 +43,8 @@ $integer = 0123; // octal number (equivalent to 83 decimal) $integer = 0x1A; // hexadecimal number (equivalent to 26 decimal) // Floats (aka doubles) -$float = 1.234; -$float = 1.2e3; +$float = 1.234; +$float = 1.2e3; $float = 7E-10; // Arithmetic @@ -74,7 +74,7 @@ $sgl_quotes END; // Nowdoc syntax is available in PHP 5.3.0 // Manipulation -$concatinated = $sgl_quotes + $dbl_quotes; +$concatenated = $sgl_quotes + $dbl_quotes; ``` ### Compound @@ -237,7 +237,7 @@ while ($i < 5) { if ($i == 3) { break; // Exit out of the while loop and continue. } - + echo $i++; } @@ -247,7 +247,7 @@ while ($i < 5) { if ($i == 3) { continue; // Skip this iteration of the loop } - + echo $i++; } ``` @@ -338,11 +338,11 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as class MyClass { function myFunction() { } - + function function youCannotOverrideMe() { } - + public static function myStaticMethod() { } @@ -362,12 +362,12 @@ PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic ```php class MyClass { private $property; - + public function __get($key) { return $this->$key; } - + public function __set($key, $value) { $this->$key = $value; @@ -471,4 +471,4 @@ Visit the [official PHP documentation](http://www.php.net/manual/) for reference 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/). \ No newline at end of file +If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/). From 124273ffbe9ac370b99ca7fecd24d2656552ca9a Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:48:53 +1200 Subject: [PATCH 02/12] Correct form of string concatenation. --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 4dafe6ad..ce96b457 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -74,7 +74,7 @@ $sgl_quotes END; // Nowdoc syntax is available in PHP 5.3.0 // Manipulation -$concatenated = $sgl_quotes + $dbl_quotes; +$concatenated = $sgl_quotes . $dbl_quotes; ``` ### Compound From 85eee3929a1ccb5225f2e1d826a466e7351bbf39 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:49:58 +1200 Subject: [PATCH 03/12] Mention print is a language construct --- php.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/php.html.markdown b/php.html.markdown index ce96b457..6dcf7d59 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -97,6 +97,7 @@ $associative["One"]; // Holds the value 1 echo('Hello World!'); // Prints Hello World! to stdout. Stdout is the web page if running in a browser. print('Hello World!'); // The same as echo echo 'Hello World!'; // echo is actually a language construct, so you can drop the parentheses. +print 'Hello World!'; // So is print echo 100; echo $variable; echo function_result(); // Output the result of a function call that returns a value. More on functions later. From 34852cc8be15e4ba643001dac8c0c0ca1ac48dca Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:56:52 +1200 Subject: [PATCH 04/12] Move reference operator to site next to variable, as per docs --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 6dcf7d59..57470390 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -114,7 +114,7 @@ echo function_result(); // Output the result of a function call that returns a v $a = 1; $b = 2; $a = $b; // A now contains the same value sa $b -$a =& $b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. +$a = &$b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. ``` ### Comparison From c3cc11f2a81411acf5e500285836c8f7744546e1 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:57:25 +1200 Subject: [PATCH 05/12] Add description of typecasting --- php.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/php.html.markdown b/php.html.markdown index 57470390..d08b9220 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -144,7 +144,11 @@ echo $string + $string; // Also outputs 2 because the + operator converts the st $string = 'one'; echo $string + $string; // Outputs 0 because the + operator cannot cast the string 'one' to a number +``` +Type casting can be used to treat a variable as another type temporarily by using cast operators in parentheses. + +```php $boolean = (boolean) $integer; // $boolean is true $zero = 0; From c5fca52d14fad914a65cdb66c51dfa9e33f3b8b8 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 08:57:52 +1200 Subject: [PATCH 06/12] Make if statement result clearer --- php.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index d08b9220..a9604df3 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -190,9 +190,9 @@ if (/* test */) { } - +This is displayed if the test is truthy. - +This is displayed otherwise. ``` From 91957c7cc2b1b0ecf131071ca7c7a2808d241f34 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:03:08 +1200 Subject: [PATCH 07/12] Make comparisons clearer when type juggling --- php.html.markdown | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index a9604df3..8f319930 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -111,24 +111,29 @@ echo function_result(); // Output the result of a function call that returns a v ### Assignment ```php -$a = 1; -$b = 2; -$a = $b; // A now contains the same value sa $b -$a = &$b; // A now contains a reference to $b. Changing the value of $a will change the value of $b also, and vice-versa. +$x = 1; +$y = 2; +$x = $y; // A now contains the same value sa $y +$x = &$y; // A now contains a reference to $y. Changing the value of $x will change the value of $y also, and vice-versa. ``` ### Comparison ```php +// These comparisons will always be true, even if the types aren't the same. $a == $b // TRUE if $a is equal to $b after type juggling. -$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 after type juggling. $a <> $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 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 the values match and they 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) From be46dc081a255d85a1b1331b4fcd949196699b7c Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:11:27 +1200 Subject: [PATCH 08/12] Add example of currying --- php.html.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/php.html.markdown b/php.html.markdown index 8f319930..ae636b0a 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -300,6 +300,23 @@ function outer_function ($arg_1 = null) { // $arg_1 is optional // inner_function() does not exist and cannot be called until outer_function() is called ``` +This enables [currying](http://en.wikipedia.org/wiki/Currying) in PHP. + +```php +function foo ($x, $y, $z) { + echo "$x - $y - $z"; +} + +function bar ($x, $y) { + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; +} + +$bar = bar('A', 'B'); +$bar('C'); +``` + ### [Variable](http://www.php.net/manual/en/functions.variable-functions.php) ```php From e051d0ec65dfbf7b97324e6b4e4812afd5a3dd03 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:15:50 +1200 Subject: [PATCH 09/12] Clarify anonymous functions as callbacks as arguments --- php.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index ae636b0a..af4e2f01 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -330,7 +330,11 @@ $function_name(); // will execute the my_function_name() function Similar to variable functions, functions may be anonymous. ```php -my_function(function () { +function my_function($callback) { + $callback('My argument'); +} + +my_function(function ($my_argument) { // do something }); From c8800bff13262b1d37b4f926a35410509ef8bbc9 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:17:37 +1200 Subject: [PATCH 10/12] Replace tabs with spaces --- php.html.markdown | 76 +++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index af4e2f01..ec9c77e7 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -124,7 +124,7 @@ $x = &$y; // A now contains a reference to $y. Changing the value of $x will cha $a == $b // TRUE if $a is equal to $b after type juggling. $a != $b // TRUE if $a is not equal to $b after type juggling. $a <> $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 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. @@ -171,27 +171,27 @@ $var = null; // Null value ```php if (/* test */) { - // Do something + // Do something } if (/* test */) { - // Do something + // Do something } else { - // Do something else + // Do something else } if (/* test */) { - // Do something + // Do something } elseif(/* test2 */) { - // Do something else, only if test2 + // Do something else, only if test2 } if (/* test */) { - // Do something + // Do something } elseif(/* test2 */) { - // Do something else, only if test2 + // Do something else, only if test2 } else { - // Do something default + // Do something default } @@ -205,15 +205,15 @@ This is displayed otherwise. ```php switch ($variable) { - case 'one': - // Do something if $variable == 'one' + case 'one': + // Do something if $variable == 'one' break; case 'two': case 'three': - // Do something if $variable is either 'two' or 'three' + // Do something if $variable is either 'two' or 'three' break; default: - // Do something by default + // Do something by default } ``` @@ -223,42 +223,42 @@ switch ($variable) { ```php $i = 0; while ($i < 5) { - echo $i++; + echo $i++; } $i = 0; do { - echo $i++; + echo $i++; } while ($i < 5); for ($x = 0; $x < 10; $x++) { - echo $x; // Will echo 0 - 9 + echo $x; // Will echo 0 - 9 } $wheels = ["bicycle" => 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++; } ``` @@ -268,7 +268,7 @@ Functions are created with the ```function``` keyword. ```php function my_function($my_arg) { - $my_variable = 1; + $my_variable = 1; } // $my_variable and $my_arg cannot be accessed outside of the function @@ -288,12 +288,12 @@ A valid function name starts with a letter or underscore, followed by any number ```php function my_function_name ($arg_1, $arg_2) { // $arg_1 and $arg_2 are required - // Do something with $arg_1 and $arg_2; + // Do something with $arg_1 and $arg_2; } // Functions may be nested to limit scope function outer_function ($arg_1 = null) { // $arg_1 is optional - function inner_function($arg_2 = 'two') { // $arg_2 will default to 'two' + function inner_function($arg_2 = 'two') { // $arg_2 will default to 'two' } } @@ -335,12 +335,12 @@ function my_function($callback) { } my_function(function ($my_argument) { - // do something + // do something }); // Closure style $my_function = function() { - // Do something + // Do something }; $my_function(); @@ -352,9 +352,9 @@ Classes are defined with the ```class``` keyword. ```php class MyClass { - const MY_CONST = 'value'; + const MY_CONST = 'value'; static $staticVar = 'something'; - public $property = 'value'; // Properties must declare their visibility + public $property = 'value'; // Properties must declare their visibility } echo MyClass::MY_CONST; // Outputs "value"; @@ -367,7 +367,7 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as ```php class MyClass { - function myFunction() { + function myFunction() { } function function youCannotOverrideMe() @@ -392,16 +392,16 @@ PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic ```php class MyClass { - private $property; + private $property; public function __get($key) { - return $this->$key; + return $this->$key; } public function __set($key, $value) { - $this->$key = $value; + $this->$key = $value; } } @@ -415,12 +415,12 @@ Classes can be abstract (using the ```abstract``` keyword), extend other classes ```php interface InterfaceOne { - public function doSomething(); + public function doSomething(); } interface InterfaceTwo { - public function doSomething(); + public function doSomething(); } abstract class MyAbstractClass implements InterfaceOne @@ -481,15 +481,15 @@ Traits are available since PHP 5.4.0 and are declared using the ```trait``` keyw ```php trait MyTrait { - public function myTraitMethod() + public function myTraitMethod() { - // Do something + // Do something } } class MyClass { - use MyTrait; + use MyTrait; } $cls = new MyClass(); From 23e822bcc1c3d0c2fdee2eed474c9debc36818b1 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:19:53 +1200 Subject: [PATCH 11/12] Correct syntax error declaring final method --- php.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index ec9c77e7..491255e9 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -367,10 +367,11 @@ Classes are insantiated with the ```new``` keyword. Functions are referred to as ```php class MyClass { - function myFunction() { + function myFunction() + { } - function function youCannotOverrideMe() + final function youCannotOverrideMe() { } From caae7d9696b62ccb2ba263a43e3a94d494589584 Mon Sep 17 00:00:00 2001 From: Malcolm Fell Date: Fri, 28 Jun 2013 09:21:09 +1200 Subject: [PATCH 12/12] Use sinlge line breaks for clarity --- php.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php.html.markdown b/php.html.markdown index 491255e9..c6c70cbb 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -411,7 +411,8 @@ echo $x->property; // Will use the __get() method to retrieve the value of $prop $x->property = 'Something'; // Will use the __set() method to set the value of property ``` -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. +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 interface InterfaceOne