Sunday 15 October 2017

php - Why does empty expect T_PAAMAYIM_NEKUDOTAYIM when a non-variable is given?

itemprop="text">

define('foo',
'bar');

if (empty(foo)) {

echo
'qux';
}




rel="nofollow">http://codepad.org/G1TSK1c6
Parse error: syntax
error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM on line
4




I know that
empty() only allows variables to be passed as an argument, but
why does it expect a T_PAAMAYIM_NEKUDOTAYIM (i.e. ::) when I
give it a constant?


itemprop="text">
class="normal">Answer





The next logical thing the parser
wants is a :: because foo is not a
variable.



if (empty(foo::$bar))
{
}


Is the
only thing, that works when empty() is not passed a variable.
Your example is evaluated as empty(bar) where the parser
assumes bar to be a class name and now expects a static member
variable.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...