strcmp
COMPARES two STRINGS by the case-sensitive way.
If $str1 is less than $str2 this function retuns an integer less than ZERO.
If $str1 is equal to $str2 this function returns ZERO.
If $str1 is greater than $str2 this function returns an an integer greater than ZERO.
This function is binary-safe
<?php
int strcmp ( str $str1 , str $str2 )
where,
$str1 = The first STRING to compare
$str2 = The second STRING to compare
?>
$str1
The first STRING to be compared.
$str2
The second STRING to be compared.
EXERCISE
<?php
function str_cmp1 ($par1, $par2)
{
$stcmp12 = strcmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is GREATER than<br>' . $par2;
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is LESS than<br>' . $par2;
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is EQUAL to<br>' . $par2;
}
}
$srcm01 = 'Ad augusta per angusta';
$srcm02 = 'Ad augusta per angusta';
$srcm03 = 'angusta';
str_cmp1($srcm01, $srcm02);
echo '<br><br>';
str_cmp1($srcm01, $srcm03);
echo '<br><br>';
str_cmp1($srcm03, $srcm01);
?>
EXERCISE
<?php
function str_cmp2 ($par1, $par2)
{
$stcmp12 = strcmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is GREATER than<br>' . $par2;
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is LESS than<br>' . $par2;
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is EQUAL to<br>' . $par2;
}
}
$srcm01 = 'Augusta';
$srcm02 = 'AUGUSTA';
$srcm03 = 'angusta';
$srcm04 = 'angusta';
str_cmp2($srcm01, $srcm02);
echo '<br><br>';
str_cmp2($srcm01, $srcm03);
echo '<br><br>';
str_cmp2($srcm03, $srcm01);
echo '<br><br>';
str_cmp2($srcm03, $srcm04);
?>
EXERCISE
<?php
function str_cmp3 ($par1, $par2)
{
$stcmp12 = strcmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is GREATER than<br>' . $par2;
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is LESS than<br>' . $par2;
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is EQUAL to<br>' . $par2;
}
}
$srcm01 = 'Docendo discimus';
$srcm02 = 'Doctus cum libro';
str_cmp3($srcm01, $srcm02);
echo '<br><br>or<br><br>';
str_cmp3($srcm02, $srcm01);
?>
EXERCISE
<?php
function str_cmp4 ($par1, $par2)
{
$stcmp12 = strcmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is GREATER than<br>' . $par2;
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is LESS than<br>' . $par2;
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore, <br>' . $par1 .
'<br>is EQUAL to<br>' . $par2;
}
}
$srcm01 = 'docendo discimus';
$srcm02 = 'DOCENDO DISCIMUS';
str_cmp4($srcm01, $srcm02);
echo '<br><br>or<br><br>';
str_cmp4($srcm02, $srcm01);
?>
EXERCISE
<?php
/* Compares two strings in case-sensitive manner */
echo "Basic and Possible operations.<br>";
/* creating an array of strings to be compared */
$arrays = [
["a", "A", 'a', 'A', chr(128), chr(255), chr(256)],
["acc", "Acc", 'ac', "accc", 'acd',
"?acc", 'acc!', "$!acc", ";acc"],
["1", "0", 0, "-1", -1, NULL, "", TRUE, FALSE, "string"],
[10.5, 1.5, 9.5, 11.5, 100.5, 10.5E1, -10.5, 10, 0.5]
];
/* loop through to go each and
every element in an array
and comparing the elements
with one and other */
foreach($arrays as $str1_arr){
echo "<br><br>comparing the strings in an<br>";
print_r($str1_arr);
for ($i=0; $i<count($str1_arr); $i++){
echo "<br><br>Iteration: $i<br>";
for($j=0; $j<count($str1_arr); $j++){
echo "- strcmp of '$str1_arr[$i]' and '$str1_arr[$j]' is => ";
var_dump(strcmp($str1_arr[$i], $str1_arr[$j]));
}
}
}
echo "<br><br>Testing miscellaneous inputs<br>";
echo "<br><br>Testing objects:<br>";
/* we get "Recoverable fatal error:
saying Object of class could not be converted
to string" by default, when an object
is passed instead of string.
The error can be avoided by choosing the
__toString magix method as follows: */
class string1 {
function __toString() {
return "Hello, world";
}
}
$obj_string1 = new string1;
class string2 {
function __toString() {
return "Hello, world\0";
}
}
$obj_string2 = new string2;
var_dump(strcmp("$obj_string1", "$obj_string2"));
echo "<br><br><br>Testing arrays:<br>";
$str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!");
var_dump(strcmp("hello?world,!$%**()%**[][[[&@#~!",
"$str_arr[1]"));
echo '<br><br>';
var_dump(strcmp("hello?world,!$%**()%**[][[[&@#~!",
"$str_arr[2]"));
echo "<br><br><br>Testing a longer and heredoc string:<br>";
$string = <<<EOD
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
@#$%^&**&^%$#@!~:())))
((((&&&**%$###@@@!!!~~~~@###$%^&*
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
EOD;
var_dump(strcmp($string, $string));
echo '<br><br>';
var_dump(strcmp($string, "xyz0123456789"));
echo '<br><br>';
var_dump(strcmp($string, "&&&"));
echo "<br><br><br>Testing a heredoc null string:<br>";
$str = <<<EOD
EOD;
var_dump(strcmp($str, "\0"));
echo '<br><br>';
var_dump(strcmp($str, NULL));
echo '<br><br>';
var_dump(strcmp($str, "0"));
echo "<br><br><br>Testing simple and complex syntax strings:<br>";
$str = 'world';
/* Simple syntax */
var_dump(strcmp("Hello, world", "$str"));
echo '<br><br>';
var_dump(strcmp("Hello, world'S", "$str'S"));
echo '<br><br>';
var_dump(strcmp("Hello, worldS", "$strS"));
echo '<br><br>';
/* String with curly braces, complex syntax */
var_dump(strcmp("Hello, worldS", "${str}S"));
echo '<br><br>';
var_dump(strcmp("Hello, worldS", "{$str}S"));
echo "<br><br><br>Testing binary safe and binary chars:<br>";
var_dump(strcmp("Hello\0world", "Hello"));
echo '<br><br>';
var_dump(strcmp("Hello\0world", "Helloworld"));
echo '<br><br>';
var_dump(strcmp("\x0", "\0"));
echo '<br><br>';
var_dump(strcmp("\000", "\0"));
echo '<br><br>';
var_dump(strcmp("\x00", ""));
echo '<br><br>';
var_dump(strcmp("\x00", NULL));
echo '<br><br>';
var_dump(strcmp("\000", NULL));
echo "<br><br><br>Comparing long float values:<br>";
/* Here two different outputs,
which depends on the rounding value
before converting to string.
Here Precision = 12 */
var_dump(strcmp(10.55555555555555555555555555,
10.5555555556));
// int(0)
echo '<br><br>';
var_dump(strcmp(10.55555555555555555555555555,
10.555555556));
// int(-1)
echo '<br><br>';
var_dump(strcmp(10.55555555595555555555555555,
10.555555556));
// int(0)
?>