I have the following code where I try to assign a value to a variable and then evaluate if it's empty:
if(!empty($user = User::find($id))){
// do some operations on the $user
}
But I'm getting an error... Does the PHP compiler allows this in any way?
PS. I'm using PHP 5.3
Generally if a function cant find something, ie a User, then it would return false
anyway - so this is what I normally do:
if($user = User::find($id))
// do some operations on the $user
}
else
{
// no user found
}
Does the PHP compiler allows this in any way?
Check out manual page about empty:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error.
In your code you have assignment operation instead of plain variable.