What PHP Function is used to find position of a string?

Use the strpos() function. It returns the position of the first occurrence of a text inside a string, or false if the text is not found. The counting starts from 0.

<?php
echo strpos("I like apple and banana", "apple");
?>

Output:

Tutorials Class - Output Window

7

The word “apple” starts at position 7, because “I like ” has 7 characters (0 to 6). Because the result can be 0 for a match at the start, always check with !== false. Learn more in PHP strpos() Function and how to check if a string contains a substring.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.