Home  >  Article  >  Backend Development  >  How to use strcmp() function to compare strings in PHP

How to use strcmp() function to compare strings in PHP

autoload
autoloadOriginal
2021-04-14 16:41:532702browse

How to use strcmp() function to compare strings in PHP

<span style="font-size: 14px;"> Comparative size of integer and floating point data in PHP</span>, It is more convenient, but it is more troublesome to compare strings. How to compare string data types, PHP provides the built-in function strcmp() for easy use.

Syntax:

strcmp(string $str1 , string $str2 )
  • $str1: The first string.

  • $str2: The second string.

Equal case

<?php 
$a=strcmp("Hello world!","Hello world!");
echo $a."<br>"; //0
?>

Greater than case

<?php 

$b=strcmp("Hello world!","Hello");
echo $b."<br>"; // 7
?>

If it is less than

<?php 
$c=strcmp("Hello world!","Hello world! Hello!");
echo $c."<br>"; // -7
?>

The comparison of the strcmp() function is based on the ASCII code size.

Recommendation: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of How to use strcmp() function to compare strings in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn