문제

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