diff options
Diffstat (limited to 'php.html.markdown')
-rw-r--r-- | php.html.markdown | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/php.html.markdown b/php.html.markdown index 5bc2ddce..6944390c 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 */ @@ -716,7 +738,7 @@ $cls = new SomeOtherNamespace\MyClass(); /********************** * Late Static Binding * -* / +*/ class ParentClass { public static function who() { @@ -748,6 +770,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 |