Jump to: navigation, search

Php strncmp

From w3cyberlearnings

Contents

PHP function strncmp

This function is a binary safe string comparision of two strings based on the first n characters length.

Syntax strncmp

  • string1: string input 1
  • string2: string to compare with string1.
  • length: number of characters use for comparision for both strings.
strncmp(string1,string2, length);

Return

  • 0: when two string equal.
  • >0: string1 is greater than string2.
  • <0: string2 is greater than string1.

Example 1

  • 'Great' is greater than 'GREAT', Uppercase character is always consider smaller than lowercase.
<?php

echo strncmp("Great","GREAT",4);
?>

Output

1

Example 2

<?php

$aa_list = array(
	 'Fish',
	 'Fun',
	 'Find',
	 'File',
	 'Fight',
	 'Fund',
	 'Function'
);

$aa_search = array();
foreach ($aa_list as $word) {
	$aa_search[$word] = strncmp($word, 'Fun', 3);
}
print_r($aa_search);
?>

Output

Array
(
    [Fish] => -1
    [Fun] => 0
    [Find] => -1
    [File] => -1
    [Fight] => -1
    [Fund] => 0
    [Function] => 0
)

Related Functions


Navigation
Web
SQL
MISC
References