Viewed   83 times

In PHP, I have error_reporting set to report everything including notices.

Why does the following not throw any notices, errors or anything else?

$myarray = null;
$myvalue = $myarray['banana'];

Troubleshooting steps:

$myarray = array();
$myvalue = $myarray['banana'];
// throws a notice, as expected ?

$myarray = (array)null;
$myvalue = $myarray['banana'];
// throws a notice, as expected ?

$myarray = null;
$myvalue = $myarray['banana'];
// no notice or warning thrown, $myvalue is now NULL. ? Why?

It's possible it's a bug in PHP, or I'm just not understanding something about how this works.

 Answers

5

There is an active bug report started at 2006.

And in documentation it is a notice about this in String section.

Saturday, December 24, 2022
4

Quote from the manual:

Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Additional details about how keys are cast are as follows:

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

As for this being useful or necessary, this is debatable. You are asked to use integer or string keys and you have been warned about implicit key casting.

Monday, August 8, 2022
 
bhawan
 
3

Here we are using foreach to iterate over values and using $value for non-empty $value["fcmToken"]

$result=array();
foreach($array as $key => $value)
{
    if(!empty($value["fcmToken"]))
    {
        $result[]=$value;
    }
}

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [fcmToken] => dfqVhqdqhpk
        )

    [1] => Array
        (
            [fcmToken] => dfgdfhqdqhpk
        )

)
Saturday, September 17, 2022
 
mehdi
 
2

You have to declare $rescntryvals as array before. Per default all variables are of type null (undefined) until you define them.

$rescntryvals  = array();
$rescntryvals[]=$rescntry;
Tuesday, October 25, 2022
1

This happens if you use C++ compiler instead of C compiler. As C++ requires explicit casting. The problem is not just with (un)casting malloc result, but any void pointer to other pointer.

Monday, August 15, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :