Those pits that PHP stepped on (2) the strpos caused by the murders

Source: Internet
Author: User
Tags chop

There's a pit ahead.

PHP some of its own functions, if used improperly, will also pit you Shingma. For example: Strpos ()

Let's look at what the Strpos () function is doing.

strpos-find where the string first appears

Usage: int Strpos (String $haystack, mixed $needle [, int $offset = 0])

Look at the following code:

$a = ' ABCD ';

$b = ' B ';

$c = ' a ';

Echo strpos ($a, $b)? ' It is brother <br/> ': ' Not my race, chop ta<br/> ';

Echo strpos ($a, $c)? ' It is brother <br/> ': ' Not my race, chop ta<br/> ';

Echo strpos ($a, $c)!=false? ' Turns out to be brothers ': ' Not my race, chop Ta ';

Results show

It's a brother.

Not my race, chop ta

Not my race, chop ta

$c name is a part of $ A, the result as a heterogeneous, connected to be cut two times, wronged, triggered a this is the same root of life, fry what too urgent murder tragedy!

Second, anti-crater Raiders

Stripos () returns the position where the string appears in another string, which is calculated starting at 0. $c the position that appears in $ A is 0 and is therefore chopped.

So, the right judgment should be:

$a = ' ABCD ';

$c = ' a ';

Echo strpos ($a, $c)!==false? ' Turns out to be brothers ': ' Not my race, chop Ta ';

Third, why there is a pit

Anti-crater Raiders said a number of reasons, here first fully understand the Strpos () function said.

Usage: int Strpos (String $haystack, mixed $needle [, int $offset = 0])

Parameters:

    • The haystack is searched in the string.

    • Needle if needle is not a string, it is converted to an integer and is treated as a sequential value of the character.

    • The offset optional offset parameter can be used to specify which character in the haystack to start the lookup. The number position returned is relative to the starting position of the haystack.

return value

Returns position information as an integral type. If Needle,strpos () is not found, a Boolean value of FALSE is returned.

In other words, this function may return a Boolean value of false, but it may also return a non-Boolean value that is equivalent to false, such as 0 or "" (empty string). Therefore, in the judgment, you need to use constant equals ' = = = ' or constant is not equal to ' = = = ' To judge, must not save a ' = ' number.

Four, anti-crater expansion:

1. Similar functions also have

Strrpos ()-Calculates the position of the last occurrence of the specified string in the target string

Stripos ()-Find where the string first appears (case insensitive)

Strripos ()-Calculates the location of the last occurrence of the specified string in the target string (case insensitive)

2. Determine if a string has a function in another string: Strstr (). A little different from strpos () is that the return value of the function is false or part of the string. If you simply judge the inclusion relationship of two strings, it is best to use Strpos, because it is faster and consumes less memory.

Those pits that PHP stepped on (2) the strpos caused by the murders

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.