5

When coding with isset i am getting an fatal error.I have searched stackoverflow but results are not satisfactory.

I am getting

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

My codes are

if (!isset( $size || $color )) {
    $style = '';    
}else{
    $style = 'font-size : ' . $size . ';color:' . $color;   
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
Musa Muaz
  • 714
  • 2
  • 11
  • 29
  • 3
    You're trying to do `$size || $color` and then see if that's set: use `if (!isset( $size ) || !isset( $color )) {` – Mark Baker Aug 19 '16 at 14:00
  • 3
    Possible duplicate of [Isset expression error](http://stackoverflow.com/questions/23205968/isset-expression-error) **or** [Compile Error: Cannot use isset() on the result of an expression](http://stackoverflow.com/questions/29636880/compile-error-cannot-use-isset-on-the-result-of-an-expression) **or** [Fatal error: Cannot use isset() on the result of an expression (you can use “null !== expression” instead)](http://stackoverflow.com/questions/32061024/fatal-error-cannot-use-isset-on-the-result-of-an-expression-you-can-use-nul) – castis Aug 19 '16 at 14:03

3 Answers3

8

As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset.

You can use multiple isset calls, or reverse the logic of your if/else block and pass multiple parameters to isset, which i think is the cleanest solution:

//true if both are set
if(isset($size, $color)) {
    $style = 'font-size : ' . $size . ';color:' . $color;
}else{
    $style = '';
}

You can clean this up a little further by setting the default value first, thus avoiding the need for an else section:

$style = '';
if(isset($size, $color)) {
    $style = 'font-size : ' . $size . ';color:' . $color;
}

You could even use a ternary, though some people find them harder to read:

$style = isset($size, $color) ? 'font-size : ' . $size . ';color:' . $color : '';
szaman
  • 2,159
  • 1
  • 14
  • 30
Steve
  • 20,703
  • 5
  • 41
  • 67
5

you should use this way

if  (!isset( $size ) || !isset( $color ))  {
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

Your expression always return either true or false => In theory isset always return true so PHP not allow this
Change

if (!isset( $size || $color )) {

To

if (!isset($size) || !isset($color)) {
KmasterYC
  • 2,294
  • 11
  • 19