Skip to content

Instantly share code, notes, and snippets.

@calvimor
Last active April 18, 2017 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvimor/c2cbc9453d8946469ed3f663f0e6d449 to your computer and use it in GitHub Desktop.
Save calvimor/c2cbc9453d8946469ed3f663f0e6d449 to your computer and use it in GitHub Desktop.
what's new in php 7
<?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)';
<?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);
<?php
//all args pass to AddItems will be in the array $items
function AddItemsToBasket(...$items) {
var_dump($items);
}
AddItemsToBasket('A', 'B', 'C');
<?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