Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top