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!';
}
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!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn