strcasecmp
COMPARES two STRINGS by the case-insensitive 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
This function uses the current LOCALE for doing the comparisons.
<?php
int strcasecmp ( 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
setlocale(LC_COLLATE, 'C');
function str_casecmp ($par1, $par2)
{
$stcmp12 = strcasecmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is GREATER than<br>' . $par2 . '<br><br>';
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is LESS than<br>' . $par2 . '<br><br>';
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is EQUAL to<br>' . $par2 . '<br><br>';
}
}
$srcm01 = 'Ad augusta per angusta';
$srcm02 = 'Ad augusta per angusta';
$srcm03 = 'angusta';
str_casecmp($srcm01, $srcm02);
str_casecmp($srcm01, $srcm03);
str_casecmp($srcm03, $srcm01);
?>
EXERCISE
<?php
function strcase_cmp2 ($par1, $par2)
{
$stcmp12 = strcasecmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is GREATER than<br>' . $par2 . '<br><br>';
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is LESS than<br>' . $par2 . '<br><br>';
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is EQUAL to<br>' . $par2 . '<br><br>';
}
}
$srcm01 = 'Ad augusta per angusta';
$srcm02 = 'AD AUGUSTA PER ANGUSTA';
strcase_cmp2($srcm01, $srcm02);
echo 'or<br><br>';
strcase_cmp2($srcm02, $srcm01);
?>
EXERCISE
<?php
function strcase_cmp3 ($par1, $par2)
{
$stcmp12 = strcasecmp($par1, $par2);
if ($stcmp12 > 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is GREATER than<br>' . $par2 . '<br><br>';
}
elseif ($stcmp12 < 0)
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is LESS than<br>' . $par2 . '<br><br>';
}
else
{
echo 'Returned value: ' . $stcmp12 .
'<br>therefore,<br>' . $par1 .
'<br>is EQUAL to<br>' . $par2 . '<br><br>';
}
}
$srcm01 = 'AD AUGUSTA PER ANGUSTA';
$srcm02 = 'Ad augusta per angusta';
$srcm03 = 'ANGUSTA';
strcase_cmp3($srcm01, $srcm02);
strcase_cmp3($srcm01, $srcm03);
strcase_cmp3($srcm03, $srcm01);
?>
EXERCISE
<?php
setlocale(LC_ALL, 'C');
/* Compares two strings in
case-insensitive manner */
echo "Basic and Possible operations.<br>";
/* creating an array of strings to be compared */
$arrays =
[
["a", 'A', chr(128), chr(255), chr(256)],
["acc", "Acc", 'aC', "acCc", 'acd', "?acc", 'Acc!', "$!acc", ";acc"],
["1", "0", 0, "-1", -1, NULL, null, "", TRUE, 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 "strcasecmp of '$str1_arr[$i]'
and '$str1_arr[$j]' is => ";
var_dump(strcasecmp($str1_arr[$i], $str1_arr[$j]));
echo '<br>';
}
}
}
echo "<br><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(strcasecmp("$obj_string1", "$obj_string2"));
echo "<br><br><br>Testing arrays:<br>";
$str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!");
var_dump(strcasecmp("hello?world,!$%**()%**[][[[&@#~!", "$str_arr[1]"));
echo '<br><br>';
var_dump(strcasecmp("hello?world,!$%**()%**[][[[&@#~!", "$str_arr[2]"));
echo "<br><br><br>Testing a longer and heredoc string:<br>";
$string = <<<EOD
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
@#$%^&**&^%$#@!~:())))((((&&&**%$###
@@@!!!~~~~@###$%^&*
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
EOD;
var_dump(strcasecmp($string, $string));
echo '<br><br>';
var_dump(strcasecmp($string, "xyz0123456789"));
echo '<br><br>';
var_dump(strcasecmp($string, "&&&"));
echo "<br><br><br>Testing a heredoc null string:<br>";
$str = <<<EOD
EOD;
var_dump(strcasecmp($str, "\0"));
echo '<br><br>';
var_dump(strcasecmp($str, NULL));
echo '<br><br>';
var_dump(strcasecmp($str, "0"));
echo "<br><br><br>Testing simple and
complex syntax strings:<br>";
$str = 'world';
/* Simple syntax */
var_dump(strcasecmp("Hello, world", "$str"));
echo '<br><br>';
var_dump(strcasecmp("Hello, world'S", "$str'S"));
echo '<br><br>';
var_dump(strcasecmp("Hello, worldS", "$strS"));
echo '<br><br>';
/* String with curly braces, complex syntax */
var_dump(strcasecmp("Hello, worldS", "${str}S"));
echo '<br><br>';
var_dump(strcasecmp("Hello, worldS", "{$str}S"));
echo "<br><br><br>Testing binary safe and binary chars:<br>";
var_dump(strcasecmp("Hello\0world", "Hello"));
echo '<br><br>';
var_dump(strcasecmp("Hello\0world", "Helloworld"));
echo '<br><br>';
var_dump(strcasecmp("\x0", "\0"));
echo '<br><br>';
var_dump(strcasecmp("\000", "\0"));
echo '<br><br>';
var_dump(strcasecmp("\x00", ""));
echo '<br><br>';
var_dump(strcasecmp("\x00", NULL));
echo '<br><br>';
var_dump(strcasecmp("\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(strcasecmp(10.55555555555555555555555555,
10.5555555556));
// int(0)
echo '<br><br>';
var_dump(strcasecmp(10.55555555555555555555555555,
10.555555556));
// int(-1)
echo '<br><br>';
var_dump(strcasecmp(10.55555555595555555555555555,
10.555555556));
// int(0)
?>