The strncmp () function in PHP compares the first 2 characters of two strings with the same method _php tips

Source: Internet
Author: User
Tags first string

The example in this article describes how the STRNCMP () function in PHP compares the first 2 characters of a two string to be equal. Share to everyone for your reference, specific as follows:

The strncmp () function in PHP is used to compare two strings (case-sensitive) to determine whether the first n characters of two strings are equal.

The strncmp () function is defined as follows:

STRNCMP (String1,string2,length)

Parameter description:

String1 required. Specify the first string to compare.
string2 required. Specify the second string to compare.
Length required. Specify the number of characters for each string used in the comparison.

Return Value Description:

If two strings are equal, the return value is 0
If string1 is less than string2, the return value is less than 0
If string1 is greater than string2, the return value is greater than 0

The sample code is as follows:

<?php
/* STRNCMP () function compares two string first n characters equal * *
 return value:
 0-if two strings are equal
 <0-if string1 is less than string2
 >0-if string1 is greater than string2
 * * *
 $str 1= "Welcome to Www.jb51.net";
 $str 2= "WELCOME to WWW.JB51.NET";
 $str 3= "Welcome to PHP";
 Echo strncmp ($str 1, $str 2,2);
 echo "<br/>";
 Echo strncmp ($str 1, $str 3,2);
>

The results of the operation are as follows:

1
0

More about PHP string operations related to the site to see the topic: "PHP string (String) Usage Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.