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

3

6

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!

Hifi answered 16/7, 2014 at 12:41 Comment(1)
if($accountid=isset($_POST['Recipient'])?$_POST['Recipient']:false){echo $accountid;}Connivent
B
7

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.

Borscht answered 16/7, 2014 at 12:44 Comment(0)
M
1

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.

Martsen answered 16/7, 2014 at 12:45 Comment(0)
P
0

You're putting an operation that doesn't return a pointer to a variable in a function that expects one.

Procopius answered 16/7, 2014 at 12:43 Comment(2)
Yes, and what should the point with if(isset($accountid =$_POST['Recipient'])) instead of if(isset($_POST['Recipient'])) be anyway?Sedition
This is wrong. If the problem was passing by reference you'd get a different error message, this is about how isset is built into PHP. 3v4l.org/6v9hh Fatal error: Only variables can be passed by reference in /in/6v9hh on line 11Heterophony

© 2022 - 2024 — McMap. All rights reserved.