Weird PHP syntax, solving!

Source: Internet
Author: User
Tags learn php

Found a PHP used to determine whether a string is the return value of the bun string and the method Strpos quite weird.

Look at the following statement:

echo "A1:". (Strpos ("CSD", "C")). "   <br> "; 0
echo "A2:". (Strpos ("CSD", "C") >=0). " <br> "; 1
echo "B1:". (Strpos ("CSD", "H", 0)). " <br> "; //
echo "B2:". (Strpos ("CSD", "H", 0) >=0). " <br> "; 1


The expected value is that the source string "ABC" contains the substring "a", where the result is the same as expected
if (strpos ("abc", "A") >=0)
{
echo "A in ABC". " <br> "; A in ABC
}
Else
{
echo "A not in ABC". " <br> ";
}


The expectation is that the source string "ABC" does not contain the substring "U", where the result is the opposite of the expected

if (strpos ("abc", "U") >=0)
{
echo "u in abc". " <br> "; u in ABC
}
Else
{
echo "U not in abc". " <br> ";
}



The expected value is that the source string "a" is smaller than the substring "ABC", and the source string "a" does not contain the substring "ABC", where the result is the opposite of the expected value

if (strpos ("A", "abc") >=0)
{
echo "ABC in a". " <br> "; //abc in a
}
Else
{
echo "ABC not in a". " <br> ";
}



The expected value is that the source string "U" is smaller than the substring "ABC", and the source string "U" does not contain the substring "ABC", where the result is opposite to the expected value

if (strpos ("U", "abc") >=0)
{
echo "ABC in U". " <br> "; ABC in U
}
Else
{
echo "ABC not in U". " <br> ";
}


The results displayed

a1:0
a2:1
B1:
b2:1
A in ABC
u in ABC
ABC in a
ABC in U



Definition and usage

The Strpos () function returns the position of the first occurrence of a string in another string.

If the string is not found, false is returned.

Grammar
Strpos (String,find,start)
Parameters Description
String Necessary. Specifies the string to be searched.
Find Necessary. Specifies the character to find.
Start Optional. Specify where to start the search.


This method of judging PHP is very different from C #, Java.
PHP is the source string does not string, the source string is less than the substring as null value returned.
After the conditional statement, if (strpos ("abc", "U") >=0) and if (Strpos ("U", "abc") >=0), the value is true.
I think this form is very weird, and beginners and people from other languages (java,c#) who turn around to learn PHP should be confused.

Ask, here, for example I want to judge the string "abc" contains "a", which method is better?


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.