On the correct use way of Strpos

Source: Internet
Author: User

Firstly, the Strpos function is introduced briefly.

The Strpos function is to find the position of a character in a string, and here it is necessary to clarify its function;

This function obtains a position , if present, returns a number, otherwise returns false;

And many times we use this function to determine whether a character is present in a string;

Some students use the posture is this;

Determine if the word ' blog ' is present in the ' Bai Jun Remote blog '
if (Strpos (' Bai Jun Remote blog ', ' Blog ')}) {
If there is execution code here
echo ' presence ';
}else{
If there is no execution code here
Echo ' does not exist ';
}

This code output is ' present '; no problem; but look at the code below;

Determine if the word ' white ' is present in the ' Bai Jun Remote blog '
if (Strpos (' Bai Jun Remote blog ', ' White ')) {
If there is execution code here
echo ' presence ';
}else{
If there is no execution code here
Echo ' does not exist ';
}

The output is ' not present ' because the ' white ' is in the No. 0 position in the ' Bai Jun ' blog, and 0 indicates false in the IF;

So, if you use Strpos to determine if a character in a string must be ===false, you must use ===false , you must use ===false

Important things to say three times, the right way to use the following;

Determine if the word ' blog ' is present in the ' Bai Jun Remote blog '
if (Strpos (' Bai Jun Remote blog ', ' blog ') ===false) {
If there is no execution code here
Echo ' does not exist ';
}else{
If there is execution code here
echo ' presence ';
}

You think you've got the whole world of strpos by using = = =? No;no;no;

This guy has a hidden pit; I'll change the word.

Determine if ' 1 ' is present in ' Bai Jun Haruka blog 1 '
if (Strpos (' Bai Jun Haruka blog 1 ', 1) ===false) {
If there is no execution code here
Echo ' does not exist ';
}else{
If there is execution code here
echo ' presence ';
}

If your brain calculates the above code output is ' exist ';

Well, you're not keeping up with my routine, it's not a question of skill;

It's time to get a word for IQ.

In fact the output is ' non-existent '; careful children's shoes will find that this 1 is without the quotation marks;

The second argument of the Strpos must be of the string type;

Therefore, if you are a strpos function that is called in loops or otherwise, and is not sure of the type of the second parameter;

So the way to insure is to use strval to turn it into a string type:

Determine if ' 1 ' is present in ' Bai Jun Haruka blog 1 '
$haystack = ' Bai Jun Haruka blog 1 ';
$needle = 1;
if (Strpos ($haystack, Strval ($needle)) ===false) {
If there is no execution code here
Echo ' does not exist ';
}else{
If there is execution code here
echo ' presence ';
}

This article for Bai Jun Remote original article, reprinted without contact with me, but please specify from Bai Jun remote blog http://www.baijunyao.com

On the correct use way of Strpos

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.