توابع strcmp() در PHP

مثال

مقایسه دو رشته (با توجه به بزرگ و کوچک بودن):

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

Run Examples

Definition and Usage

The strcmp() function compares two strings.

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

Tip:The function is similar to strncmp() Functions similar, but different is that, through strncmp(), you can specify the number of characters used for comparison in each string.

Syntax

strcmp(string1,string2)
Parameters Description
string1 Required. Specifies the first string to be compared.
string2 Required. Specifies the second string to be compared.

Technical Details

Return value:

This function returns:

  • 0 - If two strings are equal
  • <0 - If string1 Less than string2
  • >0 - If string1 Greater than string2
PHP Version: 4+

More Examples

Example 1

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

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

Run Examples

Example 2

Different return values:

<?php
echo strcmp("Hello world!","Hello world!"); // Two strings are equal
echo strcmp("Hello world!","Hello"); // string1 Greater than string2
echo strcmp("Hello world!","Hello world! Hello!"); // string1 Less than string2
?>

Run Examples