Recently, I've attempted to be tricky and assign a variable inside of an isset function. I tried to do it like so
if(isset($accountid =$_POST['Recipient']))
{
// my code here ...
}
However, when I do this I receive the error
syntax error, unexpected '=', expecting ',' or ')'
Here is the documentation for isset if you want to reference it in your answer.
bool isset ( mixed $var [, mixed $... ] )
This isn't the hugest deal - but I'd be interested to know why I can't do something along those lines!
isset is a language construct and not a true function. It is mentioned in the docs:
Warning
isset() only works with variables as passing anything else will result
in a parse error. For checking if constants are set use the defined()
function.
You are trying to pass a statement, this might be the reason. Here is a note I found in php.net manual for isset().
http://php.net/manual/en/function.isset.php
isset() only works with variables as passing anything else will result in a parse error.
You're putting an operation that doesn't return a pointer to a variable in a function that expects one.
Related
I just came across a strange situation. When I try the following code in $ php -a, I receive an error:
php > var_dump(isset(null));
PHP Fatal error: Cannot use isset() on the result of an expression
(you can use "null !== expression" instead) in php shell code on line
1
But when I do the same thing with empty(), everything is ok:
php > var_dump(empty(null));
bool(true)
Can anyone explain why I receive an error when I try isset(null)?
Update
Thank you all for your answers. I asked this question just to make sense of why isset() is behaving differently from empty().
To me, both of them are php functions and both accept a parameter. So, as any other function in php, calling isset(null) should be a valid statement. Aren't we passing null as a value to isset() function? So why php consider it as an expression?
Testing if an expression is "set" doesn't make sense. As per the manual, isset is used to
Determine if a variable is set and is not NULL.
If you want to check if an expression is null, use is_null, or as the error message suggests, null !== expression.
The manual for empty suggests something similar:
Determine whether a variable is considered to be empty.
until you read slightly further down, in the changelog:
5.5.0 empty() now supports expressions, rather than only variables.
Prior to this, empty(null) would have thrown an error along the lines of
Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in ... on line ...
Ok, I found something that I didn't know before in php which could be the answer to this question. According to the manual, isset() is not a function, but rather a language construct like if ... else, foreach and while:
Note: Because this [referring to isset()] is a language construct and not a function, it cannot be called using variable
functions.
There are a few more of these language constructs that can be easily confused with functions, including:
unset()
empty()
die()
include()
So now it makes sense why isset(null) doesn't work. We are trying to use a construct that expects a variable inside the parenthesis. Providing anything else other than a variable will result in syntax error during parsing of the code.
I know about questions like this one. There are lots of them with great answers.
I know this was "fixed" in PHP 5.5.x, but I'm unfortunately I'm using 5.3.x.
$iHatePHP = $node->get($key);
if (isset($node->get($key)) ...
The error I get:
Fatal error: Can't use method return value in write context in ...
I know the "fix" is to put the result of get() into a variable and call isset() on that. However, in order to save writing that thousands of times in my code, is it equivalent or am I missing some cases?
$iHatePHP = $node->get($key);
if (!($node->get($key)) ...
Edit: I control get(). So I can make it return anything I like, such as NULL, FALSE or ""
The isset() pseudo-function checks not for a variable that would cast to false, but for one which is null. Additionally, it checks for a variable or array key's existence; a non-existent variable would be null anyway, but would also issue a Notice, in case you had mistyped the name or similar.
When you are testing the result of a function or method call, you know that there is some return value (a function with no return statement, or a plain return; with no value, is returning null), so the extra case of "no such variable" is impossible. The easiest way to test the value is therefore is_null:
if ( is_null($node->get($key)) ) ...
If $node->get($key) returns false, 0, or '', the ! version would enter the if statement due to the rules on converting other types to boolean.
The similar empty() construct does evaluate as though you had applied a ! operator, but preserves the special behaviour for non-existent variables - empty($foo) is effectively the same as ! isset($foo) || ! (bool)$foo.
I have been looking for an error in my code since an hour. This was the error:
Writing:
if(isset(($_POST['to'])))
instead of
if(isset($_POST['to']))
I don't get why is this extra pair of brackets causing an Internal Server Error.
I don't think putting brackets around a variable never changes its value. I mean,
$a = $b;
$c = ($b);
$a==$c; //True
I am curious as to know why is it an error?
Thank you.
EDIT:
The above error was occurring for normal variable also.
This is because isset is not a function but a language construct; as such, its definition can be found in the language parser.
T_ISSET '(' isset_variables ')' { $$ = $3; }
It only expects one pair of braces; passing another pair will cause a parse error.
Im pretty sure it has something to do with the fact that isset can not take a function in parameter. You have to pass it a value. Your extra pair of parenthesis may be evaluated as a 'function' or something that need to be evaluated.
Normally, when you try to pass a function to isset, you get this error :
Can't use method return value in write context
isset:
Warning
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:
<?php
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
isset() in php 5.3 seems to be behaving unexpectedly. I have a class called DB details that encapsulates a bunch of string properties with getters and setters.
$dbdetails->getDatabasename() evaluates to a string ("mydb")
This throws a 500 error:
if(!isset($dbdetails->getDatabasename())){
//do something
}
This works fine
$databasename = $dbdetails->getDatabasename();
if(!isset($databasename)){
//do something
}
I wasn't able to see any log output because apache sent back a 500 even though the error ini param is set (sic) to On. I know this is something to do with the isset call for sure.
Any idea what could be wrong, or did I find a PHP bug?
The isset function checks whether a variable is set. Checking against $databasename is valid, because it is a variable that can be set or not. Checking against a function is invalid, because it simply isn't a variable.
You probably want to use is_null( $value ) when checking the immediate result of a function.
An example from the comments on the the is_null documentation:
<?php
function test( ) { return null; }
var_dump( is_null( test( ) ) ); // displays "true"
var_dump( isset( test( ) ) ); // parse error, because "test()" is not a variable
?>
PHP: is_null
PHP: isset
That's how isset() works. Same as empty(). They can only work with a variable, not an expression. From the documentation:
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
Therefore, isset(function_call()) is invalid syntax.
Ran into a strange problem in PHP today and I'm wondering if someone can explain it. While comparing two arrays I initially tried something like this:
echo empty(array_diff( array('foo','bar') , array('bar','foo') ))
This results in the following error:
Fatal Error: Can't use function return value in write context
Rewriting this as...
$dif = array_diff( array('foo','bar') , array('bar','foo') );
echo empty($dif);
...works perfectly. Empty should just be evaluating the value passed in to it, not writing to it, so what's going wrong here? Tested in both PHP 5.2.10 and PHP 5.3.2.
I've resolved the issue by using !count() instead of empty(), but I'm curious why it doesn't work in the first place. Is empty() trying to alter the result from array_diff?
Check the manual on empty():
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
empty(), like e.g. echo() and die(), is a language construct, and therefore has different rules than a normal function (in which your example would work fine).