How to compare strings in php regardless of size

青灯夜游
Release: 2023-03-08 14:52:01
Original
2488 people have browsed it

In PHP, you can use the built-in function strncasecmp() to compare strings without case sensitivity, the syntax is "strncasecmp(str1,str2,length)". If the return value is 0, it means that the two strings are equal; if the return value is less than 0, it means that str1 is less than str2.

How to compare strings in php regardless of size

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php case-insensitive comparison String

<?php
echo strncasecmp("Hello","Hello",6);
echo "<br>";
echo strncasecmp("Hello","hELLo",6);
?>
Copy after login

Output:

0
0
Copy after login

Because the strncasecmp() function is not case-sensitive, the output of Hello and hELLo is the same.

[Recommended learning: "PHP Video Tutorial"]

Related function introduction:

strncasecmp() Function is used to compare two strings (case insensitive).

Syntax:

strncasecmp(string1,string2,length)
Copy after login

Parameters:

  • ##string1 Required. Specifies the first string to compare.

  • #string2 Required. Specifies the second string to be compared.​

  • #length Required. Specifies the number of characters per string used for comparison.

Return value:

  • 0 - If the two strings are equal

  • <0 - If string1 is less than string2

  • >0 - If string1 is greater than string2

For more programming-related knowledge, please visit:

Programming video! !

The above is the detailed content of How to compare strings in php regardless of size. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!