PHP or Hypertext Processor is a server-side, intuitive scripting language. Like all scripting languages, PHP allows developers to handle data returned from a web browser and build logic into the web page creation. It also has several extensions that make it easy to extract data for displaying on the web page, interact with databases and store input information into the database. PHP includes an interpreter and scripting language. The scripts are embedded into web server served HTML documents. The interpreter acts as a module that integrates into the web server, converting scripts into commands executed by a computer to achieve the necessary results as defined by the web developer.
PHP offers a range of functions that make it easy to work with different data types. In this article, we are going to explore strpos in PHP.
What Are strpos in PHP?
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 upper-case and lower-case characters are treated differently as the function is case-sensitive.
For example, if you want to find the position ‘php’ in the string ‘strpos in php’, use the following:
Use of strpos
As mentioned above, the function of strpos function is to find the first occurrence of a string inside some other string. It is interesting to note that the function is binary-safe.
In other words, the strpos in PHP is used to find numeric index of needle's occurrence in the haystack string. Let's look at the syntax of the function.
The output is 7 because the first character in “strpos in PHP" is a lowercase 'i’ is at index 7.
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.
As shown in the first example, we can specify the whole word as the parameter too. 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. However, let's see what happens when the substring is not found in the string using this script:
When we try to execute this script, the output is ‘Error’ despite ‘This’ is clearly in the given string. This is not a case-sensitivity problem. Then?
“This” is the first thing in the string which means the strpos in PHP will return index 0. PHP considers 0 to be false, which means that it can't differentiate between substring found at index 0 and substring not found.
This issue can be resolved using the '===' operator called 'is identical to.' If search_string is false, it must be equal to and as of the same type as the Boolean false. Rather than using == in PHP, if we use ===, then PHP will check the value of 0 and false and find that it is not a match because the latter is a Boolean and the former is an integer. The corrected version is:
Now, let us try to find “i” in this string using this script:
Here, strpos in php matches the first “i" it comes across, i.e., in "This." However, 'i' is also present in 'in,' so if we want to get its index, we can specify where to start searching. This can be done as follows:
The output is the index of ‘i' of "is."
Parameter Values (With Their Usecase)
The strpos in php have three parameters, out of which one is optional, and two are mandatory. They are as follows:
- original_string (required)- This parameter includes the original string in which we need to search the substring.
- search_string (also required)- This parameter is the substring that we need to search in the original string.
- starting_position (optional)- It is the position of the original string from where the function starts searching the original string.
Here is an example to work with a case sensitive function:
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
In this article, we saw the strpos in PHP function, its use, and syntax. We have tried to detail the examples and showed you some corner cases. If you are interested in learning more about PHP and web development, don't forget to check out Simplilearn’s full-stack web development certification.
Simplilearn also offers a learning platform called SkillUp, where learners can take up free courses that are self-paced and have been created by top practitioners and industry experts and learn some amazing in-demand skills.