


PHP considers 0 to be false, which means that it can't differentiate between substring found at index 0 and substring not found. “This” is the first thing in the string which means the strpos in PHP will return index 0. When we try to execute this script, the output is ‘Error’ despite ‘This’ is clearly in the given string. However, let's see what happens when the substring is not found in the string using this script: strpos in PHP returns the first occurrence of that word in the string then, i.e., the index of the first letter of the matched word. Note: PHP considers the first character of the string to be at index 0, which means that the 'i’ strpos() found is the eighth character.Īs shown in the first example, we can specify the whole word as the parameter too. The output is 7 because the first character in “strpos in PHP" is a lowercase 'i’ is at index 7. Let's look at the syntax of the function. In other words, the strpos in PHP is used to find numeric index of needle's occurrence in the haystack string. It is interesting to note that the function is binary-safe. The upper-case and lower-case characters are treated differently as the function is case-sensitive.įor example, if you want to find the position ‘php’ in the string ‘strpos in php’, use the following:Īs mentioned above, the function of strpos function is to find the first occurrence of a string inside some other string. The function returns an integer value which is the index of the first occurrence of the string. Its use is to find the first occurrence of a substring in a string or a string inside another string.
