|
Posted by Jeff North on 07/05/06 11:51
On Wed, 05 Jul 2006 11:39:40 +0100, in comp.lang.php Geoff Berrow
<blthecat@ckdog.co.uk>
<ef5na2lljk8iv19fd3r2t7v87r24mtrkuk@4ax.com> wrote:
>| Message-ID: <4h19smF1ldsruU1@individual.net> from Paul Lautman contained
>| the following:
>|
>| >Could you explain your statement
>| >
>| >> can't do if( Astring == Bstring) must use strcmp()
>| >
>| >I often compare strings in an if statement?
>|
>| So do I.
I stand corrected. I just looked at my old code that was causing this
problem. All I can see is that I wasn't initialising variables
correctly before the comparison, thus the error.
>| It may be something to do with this:
>|
>| http://uk.php.net/strcmp
>|
>| From the user notes...
>|
>| When we make a comparison with == php automaticly converts strings to
>| integers when either side of the comparison is an integer, f.e.:
>| <?
>| $value = 0;
>| if($value == "submit") {
>| echo "Let's submit";
>| }
>| ?>
>| Above would be succesful, since "submit" is converted to an integer (eq
>| 0) and the equation is would return true; (that's why (1 == "1submit")
>| would also return true)
>|
>| That's why we should use strcmp or === (checks type also), for string
>| comparisons.
Maybe it's because of my old C/C++ programming days but I think I'll
stick with the strcmp(), I feel safer that way :-)
Here are some interesting links that people might find useful:
http://au.php.net/manual/en/language.operators.comparison.php
http://au.php.net/manual/en/types.comparisons.php
http://killersoft.com/misc/php_variable_tests.php
---------------------------------------------------------------
jnorthau@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
[Back to original message]
|