RE: [PHP] Sloppy coding?
| From: | Ford, Mike [LSS] | Date: | Wed, 07 May 2003 10:27:30 +0000 |
| Subject: | RE: [PHP] Sloppy coding? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-146645@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Shawn McKenzie [mailto:nospam@mckenzies.net]
> Sent: 07 May 2003 05:21
>
> I want to sidestep any pitfalls and try to be a good coder.
> So what is the
> difference between these two:
>
> if(!$somevar){
> ...do stuff
> }
This will "do stuff" if $somevar is undefined, NULL, FALSE, zero (integer 0 or float 0.0),
"", "0", or an empty array. It will also issue a notice if $somevar is undefined
-- and although you will only see this if you have error_reporting set to E_ALL, it's better
practice to write code that won't issue any messages even at the highest level of reporting.
If you want to do this test without the possibility of generating a message, use
if(empty($somevar)).
>
> if(!isset($somevar)){
> ...do stuff
> }
This will "do stuff" only if $somevar is undefined or NULL, and will not issue any
messages.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211