Re: strcmp() versus ==

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 16 Mar 2009 17:06:35 -0500, nospam@xxxxxxxxxxxxx (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','ZZZZ','001A','0000','7205','00Z0'); 	

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

And the results I got were:

Tb2_38: Original   Raw comp   String Comp
                ASDF        0000           0000
                01A3        00A3           001A
                0A13        01A3           00A3
                1               0A13           00Z0
                00A3        ASDF           01A3
                ZZZZ        ZZZZ          0A13
                001A        1                 1
                0000        001A           7205
                7205        00Z0           ASDF
                00Z0        7205           ZZZZ

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','ZZZZ','001A','0000','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 '<p>Tb2_38: Original&nbsp;&nbsp;&nbsp;Raw comp &nbsp;&nbsp;String Comp</p>'; 
	$i = 0; $k = count ($aa);
	while ($i < $k)
		{
		echo '<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '.$aa[$i].
		'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$cc[$i].
 '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$bb[$i].'</p>';
		++$i;
		}

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux