diff options
author | David Hsieh <davidhsiehlo@gmail.com> | 2016-03-11 08:39:55 -0600 |
---|---|---|
committer | David Hsieh <davidhsiehlo@gmail.com> | 2016-03-11 08:39:55 -0600 |
commit | ceb99e14019672309ca80350054b7bb1a6642a48 (patch) | |
tree | 1cca5af13a146c0a36ef760b6264d18875290ec0 /php.html.markdown | |
parent | 51c2f7ce28caf1cc654bcafc4063f3012cc2f0c3 (diff) | |
parent | 8d1e2e31ef9c62e2833ccb83cde78caef668f044 (diff) |
Merge branch 'adambard-master' into r-spanish
Diffstat (limited to 'php.html.markdown')
-rw-r--r-- | php.html.markdown | 84 |
1 files changed, 75 insertions, 9 deletions
diff --git a/php.html.markdown b/php.html.markdown index 5bc2ddce..6c2b38c8 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -53,6 +53,8 @@ $int1 = 12; // => 12 $int2 = -12; // => -12 $int3 = 012; // => 10 (a leading 0 denotes an octal number) $int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) +// Binary integer literals are available since PHP 5.4.0. +$int5 = 0b11111111; // 255 (a leading 0b denotes a binary number) // Floats (aka doubles) $float = 1.234; @@ -104,7 +106,8 @@ END; echo 'This string ' . 'is concatenated'; // Strings can be passed in as parameters to echo -echo 'Multiple', 'Parameters', 'Valid'; +echo 'Multiple', 'Parameters', 'Valid'; // Returns 'MultipleParametersValid' + /******************************** * Constants @@ -115,10 +118,12 @@ echo 'Multiple', 'Parameters', 'Valid'; // a valid constant name starts with a letter or underscore, // followed by any number of letters, numbers, or underscores. -define("FOO", "something"); +define("FOO", "something"); + +// access to a constant is possible by calling the choosen name without a $ +echo FOO; // Returns 'something' +echo 'This outputs ' . FOO; // Returns 'This ouputs something' -// access to a constant is possible by direct using the choosen name -echo 'This outputs '.FOO; /******************************** @@ -137,6 +142,9 @@ $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; echo $associative['One']; // prints 1 +// Add an element to an associative array +$associative['Four'] = 4; + // List literals implicitly assign integer keys $array = ['One', 'Two', 'Three']; echo $array[0]; // => "One" @@ -159,9 +167,9 @@ echo('Hello World!'); print('Hello World!'); // The same as echo -// echo is actually a language construct, so you can drop the parentheses. +// echo and print are language constructs too, so you can drop the parentheses echo 'Hello World!'; -print 'Hello World!'; // So is print +print 'Hello World!'; $paragraph = 'paragraph'; @@ -219,7 +227,11 @@ assert($a !== $d); assert(1 === '1'); assert(1 !== '1'); -// spaceship operator since PHP 7 +// 'Spaceship' operator (since PHP 7) +// Returns 0 if values on either side are equal +// Returns 1 if value on the left is greater +// Returns -1 if the value on the right is greater + $a = 100; $b = 1000; @@ -445,6 +457,16 @@ function parameters() { parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | +// Since PHP 5.6 you can get a variable number of arguments +function variable($word, ...$list) { + echo $word . " || "; + foreach ($list as $item) { + echo $item . ' | '; + } +} + +variable("Separate", "Hello", "World") // Separate || Hello | World | + /******************************** * Includes */ @@ -517,10 +539,23 @@ class MyClass print 'MyClass'; } - //final keyword would make a function unoverridable + // final keyword would make a function unoverridable final function youCannotOverrideMe() { } + + // Magic Methods + + // what to do if Object is treated as a String + public function __toString() { + return $property; + } + + // opposite to __construct() + // called when object is no longer referenced + public function __destruct() { + print "Destroying"; + } /* * Declaring class properties or methods as static makes them accessible without @@ -716,7 +751,7 @@ $cls = new SomeOtherNamespace\MyClass(); /********************** * Late Static Binding * -* / +*/ class ParentClass { public static function who() { @@ -748,6 +783,37 @@ I'm a ParentClass But I'm ChildClass */ +/********************** +* Magic constants +* +*/ + +// Get current class name. Must be used inside a class declaration. +echo "Current class name is " . __CLASS__; + +// Get full path directory of a file +echo "Current directory is " . __DIR__; + + // Typical usage + require __DIR__ . '/vendor/autoload.php'; + +// Get full path of a file +echo "Current file path is " . __FILE__; + +// Get current function name +echo "Current function name is " . __FUNCTION__; + +// Get current line number +echo "Current line number is " . __LINE__; + +// Get the name of the current method. Only returns a value when used inside a trait or object declaration. +echo "Current method is " . __METHOD__; + +// Get the name of the current namespace +echo "Current namespace is " . __NAMESPACE__; + +// Get the name of the current trait. Only returns a value when used inside a trait or object declaration. +echo "Current namespace is " . __TRAIT__; /********************** * Error Handling |