PHP strcmp()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Compare two strings (case-sensitive):

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

Run example »

Definition and Usage

The strcmp() function compares two strings.

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

Tip:This function is similar to thestrncmp()function, except that with strncmp() you can specify the number of characters to use for comparison from each string.


Syntax

strcmp(string1,string2)

Parameters Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to compare.

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

Compare two strings (case-sensitive, Hello and hELLo have different output):

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

Run example »

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
?>

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions