> -----Original Message-----
> From: Chris Hewitt [mailto:[EMAIL PROTECTED]
> Sent: 03 April 2003 09:26
> To: Justin French

[snip]
> 
> What I have done is to do the addslashes/stripslashes assuming 
> magic_quotes_runtime is off then force it off within my code 
> beforehand 
> with:
> 
>         if (ini_get('magic_quotes_runtime') == 1)
>         {
>             if (ini_set('magic_quotes_runtime','Off') == false)
>             {
>                 echo "ERROR: Could not turn off 
> magic_quotes_runtime\n";
>             }
>         }
> 
> I found that ini_set would through an error if the seting was already 
> made, hence the initial check. So far this seems OK, but it was only 
> yesterday...

Something bugged me about this code and comment when I very first read it,
but it's taken an overnight cogitate to work out what it was.  Dare I
venture that your previous code, which sometimes appeared to throw an error,
was simply:

          if (ini_set('magic_quotes_runtime','Off') == false)
          {
              echo "ERROR: Could not turn off magic_quotes_runtime\n";
          }

?

If so, there's a fundamental flaw here: as ini_set is defined to return the
previous value of the setting, or FALSE if it fails, a simple equality
comparison (==) to FALSE will succeed whenever the ini_set fails *or when
the previous value was 0 (off)*.  To echo your error only when ini_set()
genuinely returns FALSE, you must do an identity comparison (===).


<rant-ish>

It can *never* be good practice to make an equality (==) or non-equality
(!=) comparison with TRUE or FALSE.  Not only is this bad style, but it is
also inefficient.  Consider:

     if ($x==TRUE)

(where $x can be any arbitrary expression).  PHP must first retrieve the
value of $x, convert it to Boolean (giving TRUE or FALSE) and then compare
it to TRUE -- yielding TRUE if $x was, er, TRUE and FALSE if $x was, duh,
FALSE.  That's a whole extra unnecessary comparison, plus some obfuscation
for humans reading the code.  So just use:

    if ($x)

Taking the converse:

    if ($x==FALSE)

this isn't quite so clear-cut.  PHP will retrieve the Boolean value of $x
(TRUE or FALSE), then compare it to FALSE, yielding TRUE if $x was FALSE,
FALSE if it was TRUE.  It's not immediately clear, but this comparison will
usually be more expensive than just doing a Boolean not (!) on the
expression.  Stylistically, it's usually also clearer to read as

    if (!$x)

-- IMO, it's better to have the clue that you're looking for the inverse
value at the front, and read it as "if not $x".  (Well, I suppose you could
also write "if (FALSE==$x)", but personally I think that's just as bad as
the other way round!)

Finally, if you never have an == or != comparison to TRUE or FALSE, the rare
occasion when you actually need to do an === or !== test will be very
obvious and will alert someone reading the code (yes, even yourself several
months later!) that something special is going on.

</rant-ish>

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: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to