smarber
smarber

Reputation: 5074

Why \empty and \isset are not allowed in php

AFAIK empty and isset are normal functions. When I try the following I get a parse error

if (\empty($array)) {
   // .....
}

PHP Parse error: syntax error, unexpected 'empty' (T_EMPTY), expecting identifier (T_STRING) in

This seems strange to me because other functions http://php.net/manual/en/ref.var.php such as \is_array, \is_null, \is_object, \strpos etc... allow backslash.

Why are these (at least) two functions different?

Upvotes: 3

Views: 2546

Answers (1)

deceze
deceze

Reputation: 522016

AFAIK empty and isset are normal functions.

They are not. They are language constructs, which you can read as keywords. If they would play by the normal rules of functions, they couldn't do what they are doing. isset($foo) would always trigger an error about undefined variables, since PHP would try to resolve the variable $foo before passing its value to the function isset. That is clearly against what those language constructs are supposed to do.

Upvotes: 16

Related Questions