Vahe Galstyan
Vahe Galstyan

Reputation: 1729

Get null when dumping index of boolean

When I execute this code I get NULL without any notice.

 $a = false;
 var_dump($a[5]);

Who can explain this?

Upvotes: 2

Views: 75

Answers (3)

Stefan Krenz
Stefan Krenz

Reputation: 26

There are two PHP configuration variables which control displaying error messages.

The first one is display_errors. If it's set to On, error messages will appear in the output.

The other one is error_reporting. It defines the severity level of the errors to display.

Both settings can be changed during runtime using ini_set.

Upvotes: 0

Naincy
Naincy

Reputation: 2943

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL.

Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.

Please refer more details on this: http://php.net/manual/en/language.types.array.php

Upvotes: 2

Shishil Patel
Shishil Patel

Reputation: 3977

You are accessing Boolean variable in array form is not possible.
Index 5 you are accessing that is not available in $a that is the reason you are getting null in var_dump
If you want to create array with Boolean value then it is possible
Check below code:

<?php
    $a = array(true, false, true, true, false, false);  
    var_dump($a[5]);
?>

Upvotes: 0

Related Questions