Question
Want to know if a string contains a specific substring. For example, you want to see if an email address contains an @ .
Solution
if(strpos($_POST['email'],'@') === false) {
echo 'There was no @ in the e-mail address!';
}
Copy after login
Discussion
The return value of strpos() is the first position where the substring appears in the string
If there is no substring in the string, strpos() will return false
- ##If the substring is located at the beginning of this string, strpos() will return 0 because position 0 represents the start of the string.
- In order to distinguish between 0 and false, you must use the identity operator (===) or the non-identity operator (!==)
- In the above example, === is used to compare the return value of strpos() with false. This test will only succeed if strpos() returns false. If strpos() returns 0 or any other number, the test will not succeed.
For more [PHP] string-access substring related articles, please pay attention to the PHP Chinese website!