Home >Backend Development >PHP Tutorial >The role of strpos in php
strpos() function is used in PHP to find the first occurrence of a specified substring in a string and return its index (starting from 0). If the substring is not found, -1 is returned. Usage: strpos($haystack, $needle, $offset = 0).
The role of strpos in PHP
In PHP, the strpos() function is used to find in a string Specifies the first occurrence of the substring.
Usage
strpos() function has the following syntax:
<code class="php">int strpos(string $haystack, string $needle, int $offset = 0)</code>
Return results
Example
The following example demonstrates the use of the strpos() function:
<code class="php">$string = "Hello World!"; // 查找子字符串 "World" 在字符串中的位置 $position = strpos($string, "World"); // 输出结果 echo $position; // 输出:6</code>
In this example, the strpos() function finds The position of the first occurrence of the substring "World" in the string (index 6).
Note
The above is the detailed content of The role of strpos in php. For more information, please follow other related articles on the PHP Chinese website!