Latest web development tutorials

PHP strcmp () function

PHP String Reference PHP String Reference

Examples

Compares two strings (case sensitive):

<?php
echo strcmp("Hello world!","Hello world!");
?>

Running instance »

Definition and Usage

strcmp () function compare two strings.

Note: strcmp () function is binary safe and case-sensitive.

Tip: This function strncmp () function is similar, the difference is that) you can specify the number of characters in each string for comparison by strncmp (.


grammar

strcmp( string1,string2 )

参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。

technical details

return value: This function returns:
  • 0 - if the two strings are equal
  • <0 - if string1 is less than string2
  • > 0 - if string1 is greater than string2
PHP version: 4+


More examples

Example 1

Compares two strings (case sensitive, Hello and hELLo output are not the same):

<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>

Running instance »

Example 2

Different return values:

<?php
echo strcmp("Hello world!","Hello world!"); // the two strings are equal
echo strcmp("Hello world!","Hello"); // string1 is greater than string2
echo strcmp("Hello world!","Hello world! Hello!"); // string1 is less than string2
?>

Running instance »


PHP String Reference PHP String Reference