This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function gen() | |
| { | |
| yield 1; | |
| yield 2; | |
| yield from gen2(); | |
| } | |
| function gen2() | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $gen = (function() { | |
| yield 1; | |
| yield 2; | |
| return 3; | |
| })(); | |
| foreach ($gen as $val) { | |
| echo $val, ' '; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Pre PHP 7 code | |
| use some\namespace\ClassA; | |
| use some\namespace\ClassB; | |
| use some\namespace\ClassC as C; | |
| use function some\namespace\fn_a; | |
| use function some\namespace\fn_b; | |
| use function some\namespace\fn_c; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class A { | |
| private $x = "1"; | |
| } | |
| // Pre PHP 7 code | |
| $getX = function() {return $this->x;}; | |
| $getXCB = $getX->bindTo(new A, 'A'); // intermediate closure | |
| echo $getXCB(); // outputs "1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| echo "\u{aa}"; // ª | |
| echo "\u{0000aa}"; // ª (the same but with optional leading 0's) | |
| echo "\u{9999}"; // 香 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface Logger { | |
| public function log(string $msg); | |
| } | |
| class Application { | |
| private $logger; | |
| public function getLogger(): Logger { | |
| return $this->logger; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| define('ANIMALS', [ | |
| 'dog', | |
| 'cat', | |
| 'bird' | |
| ]); | |
| echo ANIMALS[1]; // outputs "cat" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Integers | |
| echo 1 <=> 1; // 0 | |
| echo 1 <=> 2; // -1 | |
| echo 2 <=> 1; // 1 | |
| // Floats | |
| echo 1.5 <=> 1.5; // 0 | |
| echo 1.5 <=> 2.5; // -1 | |
| echo 2.5 <=> 1.5; // 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Fetches the value of $_GET['user'] and returns 'nobody' | |
| // if it does not exist. | |
| $username = $_GET['user'] ?? 'nobody'; | |
| // This is equivalent to: | |
| $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; | |
| // Coalescing can be chained: this will return the first | |
| // defined value out of $_GET['user'], $_POST['user'], and | |
| // 'nobody'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function arraysSum(array ...$arrays): array | |
| { | |
| return array_map(function(array $array): int { | |
| return array_sum($array); | |
| }, $arrays); | |
| } | |
| print_r(arraysSum([1,2,3], [4,5,6], [7,8,9])); | |
| ?> |