I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?
strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.
The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string. 2. It is case-sensitive function.
The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.
From the PHP online manual:
If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function
strpos()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With