Last active
April 18, 2017 02:03
-
-
Save calvimor/c2cbc9453d8946469ed3f663f0e6d449 to your computer and use it in GitHub Desktop.
what's new in php 7
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 | |
if(isset($_GET['name'])) { | |
$name = $_GET{'name']; | |
} | |
else { | |
$name = '(unknown)'; | |
} | |
//Other option to get name | |
$name = $_GET['name'] ?: '(unknown)'; | |
//there is a trouble if name is not set in the url | |
echo htmlspecialchars($name); | |
//Coalesce it's kind of like a ternary operator, but it checks for isset | |
$name = $_GET['name'] ?? '(unknown)'; | |
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 | |
//if the first operant is less than the second one, we get -1, | |
//if the first operant is larger than the second one, we get 1, | |
//if both operant are the same, we get 0 | |
$orderNumbers = [1, 4, 7, 34 ]; | |
usort($orderNumber, function($p1, $p2) { | |
return $p1 <=> $p2; | |
}); | |
var_dump($orderNumbers); |
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 | |
//all args pass to AddItems will be in the array $items | |
function AddItemsToBasket(...$items) { | |
var_dump($items); | |
} | |
AddItemsToBasket('A', 'B', '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 | |
declare(strict_types=1); | |
function calcTotal (float $price, float $shipping) :float { | |
return $price + $shipping; | |
} | |
//Type error because first param is a string | |
$calcTotal = calcTotal('1.23', 4.56); | |
var_dump($calcTotal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment