11

I upgraded to PHP 8.0 a few months ago and like a lot of developers I'm having trouble with this notice turned into a warning. I don't understand the proper way to deal with this so I'd like to know how to fix this without bloating my code.

Previously I would do this:

public function myFunction($options = []){

    //we know $options is an array, so let's check if the key is there and get its value at the same time
    if($run = $options['run']){ 
            
        //do something with $run
            
    }
}

$options is initialized as an array and if key 'run' is defined I get its value ready to be used. What it's being suggested I handle with this simple piece of code now is...

public function myFunction($options = false){

    if(isset($options['run']){

        $run = $options['run'];
            
        //do something with $run
            
    }
}

It seems to me like this is adding an extra unneeded step: is it really how it should be handled?

2 Answers 2

25

You can do this to avoid typical if/else block

$run = $options['run'] ?? null;

To know more details https://wiki.php.net/rfc/isset_ternary

Sign up to request clarification or add additional context in comments.

2 Comments

This solution is not backward compatible. Any other way?
This is compatible since PHP 7.0 but still much less readable in simple cases like if (!$_POST['delete']). If this is the best "official" solution, I'll use the @ prefix for all array accesses now. PHP gets worse with every version. Not much and its advantages over proper application environments like ASP.NET Core with C# have all vanished.
0

You can suppress the error by appending "@" before the expression. $run will be null if the key doesn't exists:

if($run = @$options['run']){ 
    //do something with $run   
}

Error Control Operators (php.net)

7 Comments

Supressing the error message DOES NOT STOP THE ERROR HAPPENING. So this is a basically bad idea in almost all cases
In practice this is a shortcut that works very well in a simple case like this: array['string']. I don't see what problem this could cause? The result is either null or the requested value.
In case the $options becomes an instance of a class, this would crash the code, you would never see the error, bad idea
This is a very bad practice. The answer above (stackoverflow.com/a/70549387/1797452) is the way to go.
This is the best way for array accesses where the key might not be set. PHP is a dynamic scripting language, not a statically-typed programming language. Not even JavaScript generates anything near a warning and just gives you undefined when a property doesn't exist. The only possible "error" that can happen here is that the key does not exist, but that's exactly the non-error I want to ignore and use the null value instead. This has worked perfectly in the past, let's keep it that way or choose another language with less typing effort.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.