PHP empty () weird behavior

The following code (# 1):

var_dump($myObject->getBook()->getCollection());
$testArray=Array();
var_dump($testArray);
var_dump(empty($testArray));

... will output:

array(0) { } array(0) { } bool(true)

The following code (# 2):

var_dump($myObject->getBook()->getCollection());
$testArray=Array();
var_dump($testArray);
var_dump(empty($myObject->getBook()->getCollection()));

... will output:

Nothing. There is no mistake, not just one character. There is nothing.

class Book{
  protected $bidArray=Array();
  public function getCollection(){
    return $this->bidArray;
  }
}

What is happening there?

+3
source share
4 answers

empty()is not a function, although it looks like one. This is just a special syntax that only works with variables, for example. empty($abc). You simply cannot use expressions such as empty(123)or empty($obj->getSth()).

+7
source

You cannot use empty()with anything other than a variable (this means that a function call is also not required).

var_dump(empty($myObject->getBook()->getCollection()));

You must disable the display of your error, as shown below:

<?php

class Bar {
        function foo() {
        }
}

$B = new Bar();
empty($B->foo());

gives

PHP : D:\cw\home\andreas\test\empty.php 9

: D:\cw\home\andreas\test\empty.php 9

.

ini_set('display_errors', true) var_dump ,

+3

php.net

empty() , . , : ( ($ )).

, empty() , .

+2

empty(), . :

+2

All Articles