Weird PHP syntax, solving!
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")). "
"; 0
echo "A2:". (Strpos ("CSD", "C") >=0). "
"; 1
echo "B1:". (Strpos ("CSD", "H", 0)). "
"; //
echo "B2:". (Strpos ("CSD", "H", 0) >=0). "
"; 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". "
"; A in ABC
}
Else
{
echo "A not in ABC". "
";
}
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". "
"; u in ABC
}
Else
{
echo "U not in abc". "
";
}
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". "
"; //abc in a
}
Else
{
echo "ABC not in a". "
";
}
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". "
"; ABC in U
}
Else
{
echo "ABC not in U". "
";
}
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?