$values = array(1, 2, 3, 4, 5, 6, 7, 8);
$even = array_filter($values, function($value) {
return $value % 2 == 0;
});
$values = array(1, 2, 3, 4, 5, 6, 7, 8);
$calculator = new Calculator();
$double = array_map(function($value) use ($calculator) {
return $calculator->multiply($value, 2);
});
function doStuff($value) {
$value = $value ?: 'default';
// ...
}
$di = new DateInterval('P7D');
$date = new DateTime();
$date->add($di);
trait alphaTrait {
function alpha() {
return 'alpha'
}
}
trait betaTrait {
function beta() {
return 'beta';
}
}
class First {
use alphaTrait;
}
class Second {
use betaTrait;
}
class Third {
use alphaTrait;
use betaTrait;
}
$a = [1, 2, 3, 4];
$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
class Repository {
/**
* @return array
*/
function getPosts() {
// ...
}
function firstPost() {
return $this->getPosts()[0];
}
}
function unixtime() {
return (new DateTime())->format('u');
}
$ php -S localhost:8000
function even($max = 10) {
$value = 0;
while ($value <= $max) {
yield $value;
$value += 2;
}
}
$generator = even(8);
foreach ($generator as $number) {
// 2, 4, 6, 8
}
try {
risky();
} catch (Exception $e) {
log('error', $e->getMessage());
} finally {
always();
}
// returns [ [ 'option', 'value' ], ... ]
$options = $settings->all();
foreach ($options as list($key, $value)) {
// ...
}
password_hash('monkey', PASSWORD_DEFAULT);