RE: [PHP] login problem fixed!!
| From: | Ford, Mike [LSS] | Date: | Thu, 25 Mar 2004 10:05:29 +0000 |
| Subject: | RE: [PHP] login problem fixed!! | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-181466@lists.php.net to get a copy of this message | ||
On 25 March 2004 09:32, Andy B wrote:
> the final line then is: if($result==false) {//test the query
> itself..if false then
> print whatever
> } else {//if it did work then do this...}
>
> but of course it could always be turned around to:
> if($result==true) {...} but dont know what way is better or
> if it is a personal choice...
Well, which way round you do it is pretty much personal preference, but on a more general note
whenever you find yourself writing a test like $x==true or $x==false, you should stop and rethink it
because there's something wrong.
Either:
(a) you should be using the === operator, because it matters whether the value is actually a Boolean
true or false, and not just some other non-Boolean value that is taken as equivalent to true or
false.
Or:
(b) you are writing inefficent and less readable code. Consider what happens in the following cases:
(i) if ($x==true)
- PHP retrieves the value of $x and converts it to Boolean
- compares it to the Boolean value true
- if it was true uses, er, true; if not, uses false
(ii) if ($x)
- PHP retrieves the value of $x and converts it to Boolean
- and uses it
Why go to the trouble of forcing PHP to do a comparison just to produce the value it had already
thought of, when you can just use it as is?
The scenario is similar with if ($x==false), except that the alternative if(!$x) ("if not
x") performs a nice efficient Boolean not instead of the more expensive comparison.
This lean, mean approach also tends to lead to better variable naming, and code which is readable in
a more natural fashion; for example:
if ($is_raining) open_umbrella();
is closer to the natural "if it's raining, open your umbrella" than:
if ($raining==true) open_umbrella();
</rant> ;)
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@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211