質問

In PHP I have the following code:

$my_var = $_GET['my_var'] or $my_var = 'empty';

Which gives a me a default value to fall back on if $_GET['my_var'] is not set and that works just fine. Although this does work as I want it to, it still generates an undefined variable warning in the error log.. is that acceptable or should I strive to never have anything go into the error log? If so can I suppress this error?

In the past I've used such functions as isset() or empty() but I like the closeness of this code to python's try.

役に立ちましたか?

解決

A better way to set $my_var could be:

$my_var = isset($_GET['my_var']) ? $_GET['my_var'] : "empty";

You definitely don't want to pollute your error logs with unnecessary messages. It makes it hard to find errors you actually want to debug, and is probably making the interpreter work hard than it needs to.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top