note 92372 added to function.strcmp
| From: | RobWiesler at osu1 dot php dot net | Date: | Tue, 21 Jul 2009 19:48:21 +0000 |
| Subject: | note 92372 added to function.strcmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158559@lists.php.net to get a copy of this message | ||
One big caveat - strings retrieved from the backtick operation may be zero terminated (C-style), and
therefore will not be equal to the non-zero terminated strings (roughly Pascal-style) normal in PHP.
The workaround is to surround every `` pair or shell_exec() function with the trim() function. This
is likely to be an issue with other functions that invoke shells; I haven't bothered to check.
On Debian Lenny (and RHEL 5, with minor differences), I get this:
====PHP====
<?php
$sz =
pwd;
$ps = "/var/www";
echo "Zero-terminated string:<br />sz = ".$sz."<br />str_split(sz) =
"; print_r(str_split($sz));
echo "<br /><br />";
echo "Pascal-style string:<br />ps = ".$ps."<br />str_split(ps) = ";
print_r(str_split($ps));
echo "<br /><br />";
echo "Normal results of comparison:<br />";
echo "sz == ps = ".($sz == $ps ? "true" : "false")."<br
/>";
echo "strcmp(sz,ps) = ".strcmp($sz,$ps);
echo "<br /><br />";
echo "Comparison with trim()'d zero-terminated string:<br />";
echo "trim(sz) = ".trim($sz)."<br />";
echo "str_split(trim(sz)) = "; print_r(str_split(trim($sz))); echo "<br
/>";
echo "trim(sz) == ps = ".(trim($sz) == $ps ? "true" :
"false")."<br />";
echo "strcmp(trim(sz),ps) = ".strcmp(trim($sz),$ps);
?>
====Output====
Zero-terminated string:
sz = /var/www
str_split(sz) = Array ( [0] => / [1] => v [2] => a [3] => r [4] => / [5] => w [6]
=> w [7] => w [8] => )
Pascal-style string:
ps = /var/www
str_split(ps) = Array ( [0] => / [1] => v [2] => a [3] => r [4] => / [5] => w [6]
=> w [7] => w )
Normal results of comparison:
sz == ps = false
strcmp(sz,ps) = 1
Comparison with trim()'d zero-terminated string:
trim(sz) = /var/www
str_split(trim(sz)) = Array ( [0] => / [1] => v [2] => a [3] => r [4] => / [5] =>
w [6] => w [7] => w )
trim(sz) == ps = true
strcmp(trim(sz),ps) = 0
----
Server IP: 209.41.74.194
Probable Submitter: 129.22.132.215
----
Manual Page -- http://www.php.net/manual/en/function.strcmp.php
Edit -- https://master.php.net/note/edit/92372
Del: integrated -- https://master.php.net/note/delete/92372/integrated
Del: useless -- https://master.php.net/note/delete/92372/useless
Del: bad code -- https://master.php.net/note/delete/92372/bad+code
Del: spam -- https://master.php.net/note/delete/92372/spam
Del: non-english -- https://master.php.net/note/delete/92372/non-english
Del: in docs -- https://master.php.net/note/delete/92372/in+docs
Del: other reasons-- https://master.php.net/note/delete/92372
Reject -- https://master.php.net/note/reject/92372
Search -- https://master.php.net/manage/user-notes.php