You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$name = @$_POST['name'];
# => null or the value for name
defaults for associative arrays
functionconnect($provided) {
$defaults = [
'host' => 'localhost',
'port' => 8000
];
$config = $provided + $defaults;
returnsome_func($config['host'], $config['port']);
}
$connection = connect([
'host' => 'some.app.server-host.com',
'port' => 9999
]);
# => falls back to localhost:8000 if config is insufficient
creating hash of empty values
$defaults = array_fill_keys(['username', 'email'], '');
# => creates a hash with passed keys having blank values
pulling values from hash and using blank defaults
functionpluck(array$keys, array$source) {
$defaults = array_fill_keys($keys, '');
returnarray_intersect_key($source, $defaults) + $defaults;
}
$data = pluck(['username', 'email', 'password'], $_POST);
# => $data will contain values for the keys, or blanks if not found
enforce a type on all array elements
declare(strict_types=1);
$input = ['one', 'two', 'three'];
$all_strings = (function (string ...$args) {
return$args;
})(...$input);
# => throws if an element is not a string
make a function only run once, and use cached result