= $paragraph ?>
2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 -```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. +assert($a == $b); // equality +assert($b != $a); // inequality +assert($a <> $b); // alternative inequality +assert($a < $c); +assert($c > $b); +assert($a <= $b); +assert($c >= $d); // 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 -``` +assert($c === $d); +assert($a !== $d); +assert(1 == '1'); +assert(1 !== '1'); -## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php) - -Variables can be converted between types, depending on their usage. - -```php - 2 $string = '1'; -echo $string + $string; -// Also outputs 2 because the + operator converts the strings to integers +echo $string + $string; // => 2 (strings are coerced to integers) $string = 'one'; -echo $string + $string; +echo $string + $string; // => 0 // 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. +// Type casting can be used to treat a variable as another type -```php -$boolean = (boolean) $integer; // $boolean is true +$boolean = (boolean) 1; // => true $zero = 0; -$boolean = (boolean) $zero; // $boolean is false +$boolean = (boolean) $zero; // => false +// There are also dedicated functions for casting most types $integer = 5; $string = strval($integer); -// There are also dedicated functions for casting most types $var = null; // Null value -``` -## [Control Structures](http://www.php.net/manual/en/language.control-structures.php) -### If Statements +/******************************** + * Control Structures + */ -```php - - + This is displayed if the test is truthy. This is displayed otherwise. -``` -### Switch statements - -```php 2, "car" => 4]; +echo "\n"; +$wheels = ['bicycle' => 2, 'car' => 4]; + +// Foreach loops can iterate over arrays +foreach ($wheels as $wheel_count){ + echo "$wheel_count"; +} // Prints "24" + +echo "\n"; + +// You can iterate over the keys as well as the values foreach ($wheels as $vehicle => $wheel_count) { echo "A $vehicle has $wheel_count wheels"; } -// This loop will stop after outputting 2 +echo "\n"; + $i = 0; while ($i < 5) { - if ($i == 3) { - break; // Exit out of the while loop and continue. + if ($i === 3) { + break; // Exit out of the while loop } echo $i++; -} +}// Prints "012" -// This loop will output everything except 3 -$i = 0; -while ($i < 5) { - if ($i == 3) { +for($i = 0; $i < 5; $i++){ + if ($i === 3) { continue; // Skip this iteration of the loop } - echo $i++; -} -``` + echo $i; +} // Prints "0124" -## Functions -Functions are created with the ```function``` keyword. +/******************************** + * Functions + */ -```php - "Hello" -Functions may be invoked by name. +// A valid function name starts with a letter or underscore, followed by any +// number of letters, numbers, or underscores. -```php - 5 +echo add(4, 2); // => 6 -// inner_function() does not exist and cannot be called until -// outer_function() is called -``` +// $result is not accessible outside the function +// print $result; // Gives a warning. -This enables [currying](http://en.wikipedia.org/wiki/Currying) in PHP. +// Since PHP 5.3 you can declare anonymous functions; +$inc = function($x){ + return $x + 1; +}; + +echo $inc(2); // => 3 -```php function foo ($x, $y, $z) { echo "$x - $y - $z"; } +// Functions can return functions function bar ($x, $y) { + // Use 'use' to bring in outside variables return function ($z) use ($x, $y) { foo($x, $y, $z); }; @@ -363,92 +358,77 @@ function bar ($x, $y) { $bar = bar('A', 'B'); $bar('C'); -``` -### [Variable](http://www.php.net/manual/en/functions.variable-functions.php) +// You can call named functions using strings +$function_name = 'add'; +echo $function_name(1, 2); // => 3 +// But, you should probably use anonymous functions instead. -```php -instanceProp = $instanceProp; + } + // Methods are declared as functions inside a class + public function myMethod() { + print "MyClass"; } final function youCannotOverrideMe() { } public static function myStaticMethod() { + print "I am static"; } } -$cls = new MyClass(); // The parentheses are optional. +echo MyClass::MY_CONST; // Outputs "value"; +echo MyClass::$staticVar; // Outputs 'static'; +MyClass::myStaticMethod(); // Outputs "I am static"; -echo MyClass::$staticVar; // Access to static vars +// Access class members using ->. +$my_class = new MyClass("An instance property"); // The parentheses are optional. +echo $my_class->property; // => "public" +echo $my_class->instanceProp; // => "An instance property" +$my_class->myMethod(); // => "MyClass" -echo $cls->property; // Access to properties -MyClass::myStaticMethod(); // myStaticMethod cannot be run on $cls -``` +// Extend classes using "extends" +class MyOtherClass extends MyClass{ + function printProtectedProperty(){ + echo $this->protprop; + } -PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic.php) for classes. + // Override a method + function myMethod() { + parent::myMethod(); + print " > MyOtherClass"; + } +} -```php -printProtectedProperty(); // => Prints "protected" +$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass" -class MyClass { +final class YouCannotExtendMe { +} + +// You can use "magic methods" to create getters and setters +class MyMapClass { private $property; public function __get($key) @@ -462,16 +442,13 @@ class MyClass { } } -$x = new MyClass(); +$x = new MyMapClass(); 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(); // Prints "I have MyTrait" + + +/******************************** + * Namespaces + */ + +// This section is separate, because a namespace declaration +// must be the first statement in a file. Let's pretend that is not the case + +/* ``` - -### [Namespaces](http://www.php.net/manual/en/language.namespaces.rationale.php) - -By default, classes exist in the global namespace, and can be explicitly called with a backslash. - ```php myTraitMethod(); ``` ## More Information