Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I assign a variable inside of isset? - php

Tags:

php

isset

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!

like image 811
Stephen Avatar asked Jul 16 '14 12:07

Stephen


People also ask

What can I use instead of isset in PHP?

You can also use ! empty() in place of isset() the function ! empty() works for both isset() and check whether the value of any string is not null, 0 or any empty string.

Why we use if isset ($_ POST submit ))?

Code explained isset( $_POST['submit'] ) : This line checks if the form is submitted using the isset() function, but works only if the form input type submit has a name attribute (name=”submit”).

What is Isset variable in PHP?

PHP isset() Function The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

What is isset ($_ GET in PHP?

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.


2 Answers

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.

like image 130
Jim Avatar answered Sep 27 '22 17:09

Jim


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.

like image 39
JayKandari Avatar answered Sep 27 '22 15:09

JayKandari