Last Updated: February 25, 2016
·
502
· horiajurcut

Setting Default Values

Instead of writing:

$params = array(
    'param_one' => null,
    'param_two' => null
);

if (isset($options['param_one'])) {
    $params['param_one'] = $options['param_one'];
}

if (isset($options['param_two'])) {
    $params['param_two'] = $options['param_two'];
}

We can use the + operator for arrays:

$params = $options + array(
    'param_one' => null,
    'param_two' => null
);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside