Why many programmers use:
if ($_GET['var'] or $this-&g开发者_JS百科t;error)
instead of
if (isset($_GET['var']) or isset($this->error)
without adding
error_reporting( error_reporting() & ~E_NOTICE )
or, (shorter but slower)
if (@$_GET['var'] or @$this->error)
...or something similar ?
Do you need to somehow (?) configure php in the ini file to suppress the errors on non-existing variables and properties to use the "feature" or is it simply a bad practice one should avoid?
or is it simply a bad practice one should avoid?
Definitely yes. It's a bad practice to suppress or ignore errors.
This doesn't seem to me like bast practice at all. As you said correctly, without setting the error_reporting, PHP would throw many warnings on specific cases (where the variables weren't set).
Using isset seems to me like the best way.
They will use it because it´s faster to type. And they may not think all problems it may cause.
what is the "correct" syntax of conditions for variables and properties that may or may not exist
Unfortunately, there is none.
Every variable used in the code should be defined first. That's the only good practice, if you want one.
So, for the class properties it's easy - it's already commonplace that every one of them should be defined in the class definition.
For the outside variables it's less obvious.
There is no single scenario.
Say, if program flow depends on the existence of GET variable, it's OK to check if it's set*
if (isset($GET['page'])) ...
note that we're not using this variable contents but only checking it's existence!
otherwise, if we expect page
to be always set, we have to check it first, and set a default value if not. and then use that new variable instead of $_GET one.
精彩评论