Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird bug when variable takes the value of a object

Tags:

variables

php

It's like this:

$var = $obj->data->field;

echo $var; // works, I get the value of 'field'

if(empty($var)) echo '$var is empty!'; // I get this message too. wtf?

What's the problem here? Why does empty() return true?

like image 523
Jan Avatar asked Dec 06 '22 20:12

Jan


1 Answers

I guess you expect that empty will return true only for NULL while actually whole set of values is considered to be "empty values"; from doc:

The following things are considered to be empty:

    * "" (an empty string)
    * 0 (0 as an integer)
    * 0.0 (0 as a float)
    * "0" (0 as a string)
    * NULL
    * FALSE
    * array() (an empty array)
    * var $var; (a variable declared, but without a value in a class)
like image 142
ain Avatar answered Dec 10 '22 11:12

ain