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

In PHP, the isset() function is used to check if a variable is set and not null. However, isset() cannot be used directly on the result of an expression, as it expects a variable as its argument.

To overcome this limitation, you can use the “null !== expression” construct instead. This construct checks if the expression is not equal to null, effectively achieving the same functionality as isset().

Here’s an example to illustrate this:

      
         $value = 10;
         
         // Using isset()
         $result1 = isset($value); // Works fine
         
         // Using isset() on an expression (Throws an error)
         $result2 = isset($value + 5); // Error: Expression expected to be a variable

         // Using "null !== expression" construct
         $result3 = null !== ($value + 5); // Works fine. Expression is not null
      
   

In the example above, $result1 will be true since $value is set and not null. However, using isset() on the expression $value + 5 will throw an error because isset() expects a variable as its argument.

To achieve the same result without using isset(), we can use the “null !== expression” construct. In this case, $result3 will also be true, since the expression $value + 5 is not null.

Similar post

Leave a comment