[PHP] Re: strcmp() versus ==

2009-03-17 Thread Shawn McKenzie
Clancy wrote:
> On Mon, 16 Mar 2009 17:06:35 -0500, nos...@mckenzies.net (Shawn McKenzie) 
> wrote:
> 
>> Shawn McKenzie wrote:
>>> Paul M Foster wrote:
 I had never completely read over the rules with regard to comparisons in
 PHP, and was recently alarmed to find that $str1 == $str2 might not
 compare the way I thought they would. Is it common practice among PHP
 coders to use strcmp() instead of == in making string comparisons? Or am
 I missing/misreading something?

 Paul

>>> I would use $str1 === $str2 if you want to make sure they are both
>>> strings and both the same value.
>>>
>>> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
>>>
>> If you want to force a string comparison you can also use:
>>
>> (string)$str1 == (string)$str2
> 
> Recently there was some discussion about inexplicable results from sorting 
> alphanumeric
> strings.  Inspired by your suggestion I filled in an array with random 4 
> character
> alphanumeric strings, and then wrote a simple bubblesort.  I made two copies 
> of the array,
> and sorted one using a simple comparison, and the other using the above 
> comparison.  The
> initial values of the array were :
> 
>   $aa = array 
> ('ASDF','01A3','0A13',1,'00A3','','001A','','7205','00Z0'); 
> 
> (There are no letter 'O's in this),
> 
> And the results I got were:
> 
> Tb2_38: Original   Raw comp   String Comp
> ASDF   
> 01A300A3   001A
> 0A1301A3   00A3
> 1   0A13   00Z0
> 00A3ASDF   01A3
>   0A13
> 001A1 1
> 001A   7205
> 720500Z0   ASDF
> 00Z07205   
> 
> Apart from the out of place '1', apparently treated as '1000', which I threw 
> in out of
> curiosity, the string comparison gave the expected results, but I cannot see 
> the logic of
> the raw comparison.  Can anybody explain these results?
> 
> Clancy
> 
> If anyone is suspicious the actual code I used was:
> 
> 
>   $aa = array 
> ('ASDF','01A3','0A13',1,'00A3','','001A','','7205','00Z0'); 
> 
>   $k = count ($aa); $bb = $aa; $cc = $aa;
>   while ($k > 1)
>   {
>   $i = 0; $j = 1; while ($j < $k)
>   {
>   if ($cc[$i] > $cc[$j])
>   {
>   $t = $cc[$i]; $cc[$i] = $cc[$j]; $cc[$j] = $t;
>   }
>   ++$i; ++$j;
>   }
>   --$k;
>   }
> 
>   $k = count ($aa);
>   while ($k > 1)
>   {
>   $i = 0; $j = 1; while ($j < $k)
>   {
>   if ((string)$bb[$i] > (string)$bb[$j])
>   {
>   $t = $bb[$i]; $bb[$i] = $bb[$j]; $bb[$j] = $t;
>   }
>   ++$i; ++$j;
>   }
>   --$k;
>   }
> 
> echo 'Tb2_38: Original   Raw comp   String 
> Comp'; 
>   $i = 0; $k = count ($aa);
>   while ($i < $k)
>   {
>   echo '    
>       '.$aa[$i].
>   ''.$cc[$i].
>  
> '   '.$bb[$i].'';
>   ++$i;
>   }

Just a quick look, your original post was about comparing "strings" and
as far as I know, unless you force a string comparison the < and > will
do a numerical comparison, and even if I'm wrong, anything compared to
your 1 will be a numerical comparison.  If your just trying to sort,
check out sort() and natsort().


-- 
Thanks!
-Shawn
http://www.spidean.com

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



[PHP] Re: strcmp() versus ==

2009-03-17 Thread Clancy
On Mon, 16 Mar 2009 17:06:35 -0500, nos...@mckenzies.net (Shawn McKenzie) wrote:

>Shawn McKenzie wrote:
>> Paul M Foster wrote:
>>> I had never completely read over the rules with regard to comparisons in
>>> PHP, and was recently alarmed to find that $str1 == $str2 might not
>>> compare the way I thought they would. Is it common practice among PHP
>>> coders to use strcmp() instead of == in making string comparisons? Or am
>>> I missing/misreading something?
>>>
>>> Paul
>>>
>> 
>> I would use $str1 === $str2 if you want to make sure they are both
>> strings and both the same value.
>> 
>> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
>> 
>
>If you want to force a string comparison you can also use:
>
>(string)$str1 == (string)$str2

Recently there was some discussion about inexplicable results from sorting 
alphanumeric
strings.  Inspired by your suggestion I filled in an array with random 4 
character
alphanumeric strings, and then wrote a simple bubblesort.  I made two copies of 
the array,
and sorted one using a simple comparison, and the other using the above 
comparison.  The
initial values of the array were :

$aa = array 
('ASDF','01A3','0A13',1,'00A3','','001A','','7205','00Z0'); 

(There are no letter 'O's in this),

And the results I got were:

Tb2_38: Original   Raw comp   String Comp
ASDF   
01A300A3   001A
0A1301A3   00A3
1   0A13   00Z0
00A3ASDF   01A3
  0A13
001A1 1
001A   7205
720500Z0   ASDF
00Z07205   

Apart from the out of place '1', apparently treated as '1000', which I threw in 
out of
curiosity, the string comparison gave the expected results, but I cannot see 
the logic of
the raw comparison.  Can anybody explain these results?

Clancy

If anyone is suspicious the actual code I used was:


$aa = array 
('ASDF','01A3','0A13',1,'00A3','','001A','','7205','00Z0'); 

$k = count ($aa); $bb = $aa; $cc = $aa;
while ($k > 1)
{
$i = 0; $j = 1; while ($j < $k)
{
if ($cc[$i] > $cc[$j])
{
$t = $cc[$i]; $cc[$i] = $cc[$j]; $cc[$j] = $t;
}
++$i; ++$j;
}
--$k;
}

$k = count ($aa);
while ($k > 1)
{
$i = 0; $j = 1; while ($j < $k)
{
if ((string)$bb[$i] > (string)$bb[$j])
{
$t = $bb[$i]; $bb[$i] = $bb[$j]; $bb[$j] = $t;
}
++$i; ++$j;
}
--$k;
}

echo 'Tb2_38: Original   Raw comp   String 
Comp'; 
$i = 0; $k = count ($aa);
while ($i < $k)
{
echo '    
    '.$aa[$i].
''.$cc[$i].
 
'   '.$bb[$i].'';
++$i;
}

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



Re: [PHP] Re: strcmp() versus ==

2009-03-17 Thread Robert Cummings
On Tue, 2009-03-17 at 08:09 +0200, Alpár Török wrote:
> 2009/3/17 Shawn McKenzie :
> > Shawn McKenzie wrote:
> >> Paul M Foster wrote:
> >>> I had never completely read over the rules with regard to comparisons in
> >>> PHP, and was recently alarmed to find that $str1 == $str2 might not
> >>> compare the way I thought they would. Is it common practice among PHP
> >>> coders to use strcmp() instead of == in making string comparisons? Or am
> >>> I missing/misreading something?
> >>>
> >>> Paul
> >>>
> >>
> >> I would use $str1 === $str2 if you want to make sure they are both
> >> strings and both the same value.
> >>
> >> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
> >>
> >
> > If you want to force a string comparison you can also use:
> >
> > (string)$str1 == (string)$str2
> >
> > --
> > Thanks!
> > -Shawn
> > http://www.spidean.com
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> AFAIK strcmp and strncmp are faster. At least for the second i
> remember seeing benchmarks that proved that.

Then you must have seen a bad benchmark. strncmp() has specific purposes
and so does strcmp() where you would choose it instead of == or ===. But
with respect to speed, if all you want to know is whether two strings
are the same then == or === will be faster since they are operators and
do not have the same overhead as a function call.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Re: strcmp() versus ==

2009-03-16 Thread Alpár Török
2009/3/17 Shawn McKenzie :
> Shawn McKenzie wrote:
>> Paul M Foster wrote:
>>> I had never completely read over the rules with regard to comparisons in
>>> PHP, and was recently alarmed to find that $str1 == $str2 might not
>>> compare the way I thought they would. Is it common practice among PHP
>>> coders to use strcmp() instead of == in making string comparisons? Or am
>>> I missing/misreading something?
>>>
>>> Paul
>>>
>>
>> I would use $str1 === $str2 if you want to make sure they are both
>> strings and both the same value.
>>
>> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
>>
>
> If you want to force a string comparison you can also use:
>
> (string)$str1 == (string)$str2
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
AFAIK strcmp and strncmp are faster. At least for the second i
remember seeing benchmarks that proved that.

-- 
Alpar Torok

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



[PHP] Re: strcmp() versus ==

2009-03-16 Thread Shawn McKenzie
Shawn McKenzie wrote:
> Paul M Foster wrote:
>> I had never completely read over the rules with regard to comparisons in
>> PHP, and was recently alarmed to find that $str1 == $str2 might not
>> compare the way I thought they would. Is it common practice among PHP
>> coders to use strcmp() instead of == in making string comparisons? Or am
>> I missing/misreading something?
>>
>> Paul
>>
> 
> I would use $str1 === $str2 if you want to make sure they are both
> strings and both the same value.
> 
> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
> 

If you want to force a string comparison you can also use:

(string)$str1 == (string)$str2

-- 
Thanks!
-Shawn
http://www.spidean.com

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



[PHP] Re: strcmp() versus ==

2009-03-16 Thread Shawn McKenzie
Paul M Foster wrote:
> I had never completely read over the rules with regard to comparisons in
> PHP, and was recently alarmed to find that $str1 == $str2 might not
> compare the way I thought they would. Is it common practice among PHP
> coders to use strcmp() instead of == in making string comparisons? Or am
> I missing/misreading something?
> 
> Paul
> 

I would use $str1 === $str2 if you want to make sure they are both
strings and both the same value.

Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.

-- 
Thanks!
-Shawn
http://www.spidean.com

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