Search code examples
phplaravelpostlaravel-5get

Why I can't use isset() with laravel $request->input('key')


These two snippets are the same

1)

if(isset($request->input('optionId'))){
  ....
  ....
  ....
  // statements will go here 
}

2)

$optionId = $request->input('optionId');

if(isset($optionId)){
  ....
  ....
  ....
  // statements will go here 
}

when first attempt, IDE says there is a syntax error. When I saved it and run the interpreter says

"Cannot use isset() on the result of an expression (you can use "null !== expression" instead)"

but the second one is executing fine.

my question is its the same when its assigned to $optionId right? Why is it running in second attempt and not in the first attempt .. what is the difference?


Solution

  • I dig some and found the real cause. Thought to share ...

    isset() is not a function. it is a language construct. Language constructs are the building blocks of a language. they are hard corded into the parser. so we cant redefine those. So isset() is hard corded into the parser to accept only variables. Thats why any statements cannot parse to isset(). if do thats the reason for the Syntax error

    functions on the other hand have mapped to a set of language constructs. parser allows to change that mapping at compile or runtime.

    summary is -> parser is the one who is dealing with language constructors and the built-in functions are mapped and simplified to a set of language constructs before parsing.