PHP Syntax for Arrays: Different Behaviour Between PHP Versions

PHP syntax for arrays: different behaviour between PHP versions

The shortened array syntax was only added in PHP 5.4: Arrays

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);

// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>

Live result

Lower versions of php complaining about brackets. Why?

In older PHP versions you have to assign result from reset (or any other function) to variable and then access it using [].

 $shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];

PHP: Strange Array Behaviour After Object Type Casting to Array

This seems to be related to bug #61655 fixed in 7.2.0:

in a object property lookup by name always in string, but in array numeric
string(like "22200" ) key will transform to numeric but not a string anymore.
when conversion internal HashTable did't changed so after conversion, key
lookup will fail.

Clarified: $a["2000"] is always interpreted as $a[2000], but (array) failed to cast object string keys to numbers. So the array contained string numeric indices, but the array syntax' automatic casting prevented those from being accessible.

usort difference php7.1 vs php5.6

The reason for this is that in this case the values get passed to the usort() callback in different order depending on the PHP version.

https://3v4l.org/bW5WP

$array = ['a', 'b'];
usort($array, function ($firstValue, $secondValue) { echo "COMPARING: {$firstValue} with {$secondValue}\n"; return -1;});

PHP 5.6 output:

COMPARING: b with a

PHP 7.x output:

COMPARING: a with b

This doesn't matter to callbacks which actually compare the given values. Your callback however always returns -1, which means first value is lesser than the second value. In PHP 5.6 this results in b being first in the list, in PHP 7.x a will be first.

Returning incosistent results from the callback will result in undefined behavior if your callback is sorting an array of more than 2 values.

Why usort reverts an array when values are equals on PHP prior to 7.0

If you want to have the same results in both versions, you will need to ensure that you specify how equal items will deal with other fields.

So you could solve this with...

usort($arr1, function ($a, $b) {
if($a['points'] == $b['points']){
return $a['date'] - $b['date'];
}
return ($a['points'] < $b['points']) ? 1 : -1;
});

So if the points value is the same, then use date as the differentiator (assuming a numeric comparison would work.)

Weird in_array() behaviour

Use third parameter of in_array to force strict match

<?php

$_aOperatorsOneOptin = array('DE-010', 'DE-005');

$bMatchPaymentOperator = in_array(0 , $_aOperatorsOneOptin, true);

if($bMatchPaymentOperator == true)
echo 'found';

php key() in foreach-loop strange behavior OR get the next key in foreach loop

According to the PHP Manual for key, key() returns the index element of the current array position.

The problem isn't so much with key, or even with the double parentheses. Key receives the array by reference, so the double parentheses aren't doing much.

The behavior comes from foreach. When foreach iterates through the array, different versions of PHP will behave different on setting the array's internal current pointer, which is what key(), next(), current(), etc, are looking at when they are called.

Arrays in PHP aren't like arrays in most languages; they are really objects (especially associative arrays). Think of them kinda like linked lists (they are not linked lists, but just go with me for illustration purposes) - when you iterate through, you need to know where you are currently at and where you are going to be next.

What is apparently happening here is that on whatever version of PHP you are running, foreach is setting the internal current pointer to the next element at the beginning of the for loop, immediately after setting the $key and $value variables in your code.

I would definitely not depend on this behavior, as subsequent updates to PHP may break this code. It's just a fun coincidence of this specific version. If you want the next key, look at replacing your foreach loop. The PHP manual on next() has good examples, and be sure to also check out prev(), each(), and the other functions in the "see also" section.



Related Topics



Leave a reply



Submit