MIKESTOWE.COM

you are here

Weekly PHP Test

What does the following code produce in PHP 5.0+

PHP
1
2
3
4
5
6
$var = (object) array('0'=>'hello world');
if(isset($var[0])) {
die('Is Set');
}
die('Not Set');

.

.

.

.

.

.

ANSWER:

Fatal Error!

In PHP trying to access an object property using array syntax throws a fatal error, however, trying to access an array key/value using object syntax will return false on isset!

Just remember anytime you have a variable that can be either an object or an array to check using the appropriate functions (is_object, is_array) before trying to access its properties.
.

PHP Manual:
http://php.net/manual/en/function.isset.php

Share this Page:
Facebook Twitter Linkedin Reddit Tumblr Email

Leave a Reply

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