0

I'm having a parse error when I upload this code on my server (php 5.5.12):

if (!empty($a = $b)) {...}

The error is :

syntax error, unexpected '=', expecting ')'

I don't have any problem with the same code on MAMP (php 5.5.10). Of course I can easily solve it with this code:

$a = $b;    
if (!empty($a)) {...}

But well, the first one should work.

Thank you!

EDIT : I works. I just had a problem with my PHP version.

1
  • 1
    Are you sure your server is 5.5.12 ? I don't think it is Commented Jun 6, 2014 at 12:56

5 Answers 5

5

From the empty() docs:

Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

Sign up to request clarification or add additional context in comments.

2 Comments

5.5.0 empty() now supports expressions, rather than only variables.
if you have php 5.5 which unless build from source is unlikely you have, most package managers ship 5.3 or 5.4
1

you can't put a variable assignment inside empty... as a shot in the dark, i guess you mean to do a comparison between $a and $b... in which case try using == or === to compare type also.

EDIT prior to PHP5.5 you will have to assign the comparison to a variable:

$expr = $a == $b;
if (!empty($expr)) {...}

HOWEVER, i'm not really sure why you need to use empty here... could just do

if (FALSE !== ($a == $b)) {...}

4 Comments

No I don't want to make the comparison. And Yes you can make a variable assignment empty(), and in simple if () {} statements.
ok then you should definitely be setting the variable prior to empty... just for good code readability.
We actually get used to it, it doesn't bother me for readability and I think we'll see more and more of that. In a lot of languages.
hmm, don't know, comparison and things i can see, but assignment, a quick glance and it will look like its supposed to be a comparison.
0

It's work fine, you can check into next url

http://sandbox.onlinephpfunctions.com/code/7ee2afa5c3dddc283b69ee9d510695acf7deb451

Maybe your server have some bad settings. Make sure you have the full version 5.5.12

1 Comment

Yes, that came from my php version. I didn't reboot my server after updating php. Thanx.
0

Well, I didn't reboot my server after updating php to 5.5.x. My bad.

But it works!

Thank you!

Comments

0

The PHP function empty() simply checks to see if a variable is both set (isset() and is not not NULL (!is_null()). In PHP versions older than 5.5, you cannot evaluate an expression inside it.

Edit:

Removed the code snippets because they were a little confusing. The second snippet in the original question is correct. The bit above should explain why.

1 Comment

Yes, and I'm telling him he has to use that form for it to work. Sorry, I'll edit to make that a little clearer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.