strncmp
COMPARES two STRINGS of the first n characters 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
This function is similar to strcmp, with the difference that you can specify the upper limit of the number of characters from each string to be used in the comparison.
<?php
int strncmp ( str $str1 , str $str2 , int $len )
where,
$str1 = The first STRING to compare
$str2 = The second STRING to compare
$len = Number of characters to br used in the comparison
?>
$str1
The first STRING to be compared.
$str2
The second STRING to be compared.
$len
Number of characters to use in the comparison.
EXERCISE
<?php
function str_ncmpN ($par1, $par2, $par3)
{
if (strncmp($par1, $par2, $par3) > 0)
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is GREATER than<br>' . substr($par2, 0, $par3);
}
elseif (strncmp($par1, $par2, $par3) < 0)
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is LESS than<br>' . substr($par2, 0, $par3);
}
else
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is EQUAL to<br>' . substr($par2, 0, $par3);
}
}
$srcnm01 = 'Ad augusta per angusta';
$srcnm02 = 'Ad augusta per angusta';
$srcnm03 = 'angusta';
$intlen = 10;
// 10 initial characters
str_ncmpN($srcnm01, $srcnm02, $intlen);
echo '<br><br>';
str_ncmpN($srcnm01, $srcnm03, $intlen);
echo '<br><br>';
str_ncmpN($srcnm03, $srcnm01, $intlen);
?>
EXERCISE
<?php
function str_ncmp ($par1, $par2, $par3)
{
if (strncmp($par1, $par2, $par3) > 0)
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is GREATER than<br>' . substr($par2, 0, $par3);
}
elseif (strncmp($par1, $par2, $par3) < 0)
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is LESS than<br>' . substr($par2, 0, $par3);
}
else
{
echo 'Returned value: ' . strncmp($par1, $par2, $par3) .
'<br>thefore,<br>' . substr($par1, 0, $par3) .
'<br>is EQUAL to<br>' . substr($par2, 0, $par3);
}
}
$srcnm01 = 'Ad augusta per angusta';
$srcnm02 = 'AD AUGUSTA PER ANGUSTA';
$srcnm03 = 'angusta';
$intlen = 10;
// 10 initial characters
str_ncmp($srcnm01, $srcnm02, $intlen);
echo '<br><br>';
str_ncmp($srcnm01, $srcnm03, $intlen);
echo '<br><br>';
str_ncmp($srcnm03, $srcnm01, $intlen);
?>
EXERCISE
<?php
echo "Testing strncmp() function:
basic functionality.<br>";
echo "<br><br>Testing strncmp()
with single quoted string:<br>";
$var01 = 'Hello';
$var02 = 'Hi';
$var01a = "Hello";
$var02a = "Hi";
echo "After: strncmp('$var01a', '$var01a', 5)<br>";
var_dump( strncmp($var01, $var01, 5) );
//expected: int(0)
echo '<br><br>';
echo "After: strncmp('$var01a', '$var02a', 5)<br>";
var_dump( strncmp($var01, $var02, 5) );
//expected: value < 0
echo '<br><br>';
echo "After: strncmp('$var02a', '$var01a', 5)<br>";
var_dump( strncmp($var02, $var01, 5) );
//expected: value > 0
echo "<br><br><br>Testing strncmp()
with double quoted string:<br><br>";
echo "After: strncmp(\"$var01a\", \"$var01a\", 5)<br>";
var_dump( strncmp($var01a, $var01a, 5) );
//expected: int(0)
echo '<br><br>';
echo "After: strncmp(\"$var01a\", \"$var02a\", 5)<br>";
var_dump( strncmp($var01a, $var02a, 5) );
//expected: value < 0
echo '<br><br>';
echo "After: strncmp(\"$var02a\", \"$var01a\", 5)<br>";
var_dump( strncmp($var02a, $var01a, 5) );
//expected: value > 0
echo "<br><br><br>Testing strncmp()
with here-doc string:<br><br>";
$str = <<<HEREDOC
Hello
HEREDOC;
echo "After: strncmp(\"$str\", \"$var01a\", 5)<br>";
var_dump( strncmp($str, $var01a, 5) );
//expected: int(0)
echo "<br><br>After: strncmp(\"$str\", \"$var02a\", 5)<br>";
var_dump( strncmp($str, $var02a, 5) );
//expected: value < 0
echo "<br><br>After: strncmp(\"$var02a\",\"$str\", 5)<br>";
var_dump( strncmp($var02a, $str, 5) );
//expected: value > 0
?>
EXERCISE
<?php
/* Test strncmp() function with
* upper-case and lower-case alphabets
* as inputs for 'str1' and 'str2' */
echo "Test strncmp() function: with alphabets.";
echo "<br><br>Passing upper-case letters for 'str1':<br>";
for($ASCII = 65; $ASCII <= 90; $ASCII++) {
echo "After: strncmp( chr($ASCII), chr($ASCII), 1 )<br>";
var_dump( strncmp( chr($ASCII), chr($ASCII), 1 ) );
//comparing uppercase letters with
//uppercase letters; exp: int(0)
echo "<br>After:
strncmp( chr($ASCII), chr($ASCII + 32), 1 )<br>";
var_dump( strncmp( chr($ASCII), chr($ASCII + 32), 1 ) );
//comparing uppercase letters with
//lowercase letters; exp: value < 0
echo '<br>';
}
echo "<br><br>Passing lower-case letters for 'str1':<br>";
for($ASCII = 97; $ASCII <= 122; $ASCII++) {
echo "After: strncmp( chr($ASCII), chr($ASCII), 1 )<br>";
var_dump( strncmp( chr($ASCII), chr($ASCII), 1 ) );
//comparing lowercase letters with
//lowercase letters; exp: int(0)
echo "<br>After:
strncmp( chr($ASCII), chr($ASCII - 32), 1 )<br>";
var_dump( strncmp( chr($ASCII), chr($ASCII - 32), 1 ) );
//comparing lowercase letters with
//uppercase letters; exp: value > 0
echo '<br>';
}
?>
EXERCISE
<?php
/* Test strncmp() function with
* double quoted strings for
* 'str1', 'str2' */
echo "Test strncmp() function: with double quoted strings.<br>";
$strings = [
"Hello, World",
"hello, world",
"HELLO, WORLD",
"Hello, World\n",
"Hello".chr(0)."World"
];
/* loop through to compare each string
with the other string */
$count = 1;
for($index1 = 0; $index1 < count($strings); $index1++) {
echo "<br><br>Iteration: $count<br>";
for($index2 = 0; $index2 < count($strings); $index2++) {
var_dump( strncmp( $strings[$index1],
$strings[$index2],
(strlen($strings[$index1]) + 1) ) );
echo '<br>';
}
$count ++;
}
?>
EXERCISE
<?php
/* Test strncmp() with various lengths */
echo "Test strncmp() function:
with different lengths.<br><br>";
/* definitions of required variables */
$str1 = "Hello, World\n";
$str2 = "Hello, world\n";
/* loop through to compare the strings,
for various length values */
for($len = strlen($str1); $len >= 0; $len--) {
echo "After: strncmp($str1, $str2, $len)<br>";
var_dump( strncmp($str1, $str2, $len) );
echo '<br>';
}
?>
EXERCISE
<?php
/* Test strncmp() function with
* binary values passed to 'str1' & 'str2'
* and with the null terminated strings */
echo "Test strncmp() function:
Checking with the null terminated strings.<br><br>";
/* A binary function should not expect
* a null terminated string, and it
* should treat input as a raw stream of data */
$str1 = "Hello\0world";
$str2 = "Hello\0";
echo "After: strncmp(\"$str1\", \"$str2\", 12)<br>";
var_dump( strncmp($str1, $str2, 12) );
//expected: int(5);
?>
EXERCISE
<?php
/* Test strncmp() function with single
quoted strings for 'str1', 'str2' */
echo "Test strncmp() function: with single quoted strings.<br>";
$strings = array(
'Hello, World',
'hello, world',
'HELLO, WORLD',
'Hello, World\n'
);
/* loop through to compare each string
with the other string */
$count = 1;
for($index1 = 0; $index1 < count($strings); $index1++) {
echo "<br><br>Iteration: $count<br>";
for($index2 = 0; $index2 < count($strings); $index2++) {
var_dump( strncmp( $strings[$index1],
$strings[$index2],
(strlen($strings[$index1]) + 1) ) );
echo '<br>';
}
$count ++;
}
?>
EXERCISE
<?php
/* Test strncmp() function with
* different strings for
* 'str1', 'str2' and
* considering case sensitive */
echo "Test strncmp() function:
with different input strings.<br><br>";
/* heredoc string */
$str1 = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* identifier name contains underscore */
$str2 = <<<identifier_str2
Example of heredoc
string, whose identifier
having underscore("_")
& numeric value.
identifier_str2;
/* identifier name starts with underscore */
$str3 = <<<_identifier_str3
Hello, World
hello, world
_identifier_str3;
/* string containing control characters */
$str4 = <<<identifier_str4
Hello, World\n
Hello\0World
identifier_str4;
$strings = array(
$str1,
$str2,
$str3,
$str4
);
/* loop through to compare
each string with the other string */
$count = 1;
for($index1 = 0; $index1 < count($strings); $index1++) {
var_dump( strncmp( $strings[$index1],
$strings[$index1],
strlen($strings[$index1]) ) );
echo '<br>';
$count ++;
}
?>
EXERCISE
<?php
/* Test strncmp() function with
* more/less number of args
* and invalid args */
echo "Testing strncmp() function:
error conditions.<br><br>";
$str1 = 'string_val';
$str2 = 'string_val';
/* Invalid argument for $len */
$len = -8;
try {
var_dump( strncmp($str1, $str2, $len) );
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>