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

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

autoload
Release: 2023-03-09 11:12:01
Original
2738 people have browsed it

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 )
Copy after login
  • $str1: The first string.

  • $str2: The second string.

Equal case

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

Greater than case

<?php 

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

If it is less than

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template