MySQL PHP =@ symbol

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 05-18-2005
Chung Leong
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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.

Reply With Quote
  #12 (permalink)  
Old 05-18-2005
Chris Hope
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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
Reply With Quote
  #13 (permalink)  
Old 05-18-2005
Tim Van Wassenhove
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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>
Reply With Quote
  #14 (permalink)  
Old 05-18-2005
Chris Hope
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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
Reply With Quote
  #15 (permalink)  
Old 05-18-2005
Chung Leong
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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.

Reply With Quote
  #16 (permalink)  
Old 05-18-2005
Philip Olson
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

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.

Reply With Quote
  #17 (permalink)  
Old 05-18-2005
Chung Leong
 
Posts: n/a
Default Re: MySQL PHP =@ symbol

If you have an argument to make, make it.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0