This is a discussion on MySQL PHP =@ symbol within the PHP Language forums, part of the PHP Programming Forums category; That's a common misconception, equating error messages with errors. E_NOTICE and E_WARNING messages do not necessarily indicate errors. Their ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
That's a common misconception, equating error messages with errors.
E_NOTICE and E_WARNING messages do not necessarily indicate errors. Their purpose is to get a programmer to take a second look at some code that could potentially be erroreous. PHP emits a E_NOTICE or E_WARNING message whenever it fails to do something (e.g. a hash table miss). Failures are not the same as errors, one has to remember. When they're expected and are handled, then there's nothing errorous in the code. I don't understand why people insist that the use of isset() is good practice. In order to see if an element is available in a hash table, PHP has to look it. So you end up doing two hash lookups to get one element. |
|
|||
|
Chung Leong wrote:
> That's a common misconception, equating error messages with errors. > E_NOTICE and E_WARNING messages do not necessarily indicate errors. > Their purpose is to get a programmer to take a second look at some > code that could potentially be erroreous. > > PHP emits a E_NOTICE or E_WARNING message whenever it fails to do > something (e.g. a hash table miss). Failures are not the same as > errors, one has to remember. When they're expected and are handled, > then there's nothing errorous in the code. > > I don't understand why people insist that the use of isset() is good > practice. In order to see if an element is available in a hash table, > PHP has to look it. So you end up doing two hash lookups to get one > element. I develop with the error reporting level to show notices so that I can ensure I don't miss using a variable that hasn't yet been assigned (and which certainly helps speed up debugging stupid coding issues). If isset() is inefficient for the reason you have stated, then what would you recommend as a more efficient method, given that you can never assume a value has actually been passed to a page in a get or post? -- Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com |
|
|||
|
On 2005-05-17, Chris Hope <blackhole@electrictoolbox.com> wrote:
> Mike Willbanks wrote: > >> Chris, >>> You could do it like this instead of using the @, although your way >>> is less verbose. >>> >>> $name = isset($_POST['name']) ? $_POST['name'] : ''; >>> >>> An advantage of doing it this way is it this way lets you specify a >>> default value. >> >> sometimes an even better way is with empty, for say if a field is >> required data :) >> >> if (empty($_POST['name'])) { >> echo('Name must be filled out.'); >> } > > That's cool. Isn't it fun how you can program in a language for 7 years > and still not know a little thing like that :) Don't know which one is fastest: - isset - empty - array_key_exists -- Met vriendelijke groeten, Tim Van Wassenhove <http://www.timvw.info> |
|
|||
|
Tim Van Wassenhove wrote:
> On 2005-05-17, Chris Hope <blackhole@electrictoolbox.com> wrote: >> Mike Willbanks wrote: >> >>> Chris, >>>> You could do it like this instead of using the @, although your way >>>> is less verbose. >>>> >>>> $name = isset($_POST['name']) ? $_POST['name'] : ''; >>>> >>>> An advantage of doing it this way is it this way lets you specify a >>>> default value. >>> >>> sometimes an even better way is with empty, for say if a field is >>> required data :) >>> >>> if (empty($_POST['name'])) { >>> echo('Name must be filled out.'); >>> } >> >> That's cool. Isn't it fun how you can program in a language for 7 >> years and still not know a little thing like that :) > > Don't know which one is fastest: > > - isset > - empty > - array_key_exists They're all essentially doing the same thing so they probably take about the same amount of time. -- Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com |
|
|||
|
Well, getting back to the OP's question, that's why we have the @
operator. If a message is bogus, just ignore it. The method posted by Good Man above is what I use. You suppress the error message from the hash access, then check the variable for content: $name = @$_POST['name']; if(!$name) { ... } More likely I would do: $name = trim(@$_POST['name']); if(!$name) { ... } To distinguish between empty string and undefined variable: $something = @$_GET['something']; if(is_null($something)) { ... } I mean that's how you'd access a hash table in C/C++: perform the look up and deal with potential null results. |
|
|||
|
E_NOTICE is an error. E_ERROR is an error. E_PARSE is an error. These
are all errors of different levels, there is no misconception here. Fact is you are promoting bad practice which is your right. Ask the php.internals list and 100% of the people polled will tell you to use isset() instead of @ here. |