Operator = with arrays in PHP

The operator = used in PHP has two different functions:

1. Add:

$value = 3;
$value += 5;
//$value = 8

2. Setting default values in an array:

$arr = array('foo' => true);
$arr += array('foo' => false);
//$arr = array('foo' => true)

As the key “foo” is already defined in the array, it does not change. On the other hand:

$arr = array();
$arr += array('foo' => false);
//$arr = array('foo' => false)

Here the pair 'foo' => false" is created by default in $arr.

Leave a Reply

Your email address will not be published. Required fields are marked *