Current Location: Home> Latest Articles> strrpos vs strpos: What’s the Difference and How to Choose in Different Scenarios?

strrpos vs strpos: What’s the Difference and How to Choose in Different Scenarios?

gitbox 2025-07-21

In PHP, the strrpos and strpos functions are both used to find the position of the first occurrence of a substring within a main string. They are very similar but have some important differences. Depending on the application scenario, we need to choose the appropriate function based on their characteristics.

1. Introduction to strpos

The strpos function is used to find the position of the first occurrence of a substring within a main string. This function returns the starting position of the substring in the main string (0-based index). If the substring is not found, it returns false.

<span><span><span class="hljs-variable">$haystack</span></span><span> = </span><span><span class="hljs-string">"Hello, world!"</span></span><span>;
</span><span><span class="hljs-variable">$needle</span></span><span> = </span><span><span class="hljs-string">"world"</span></span><span>;
</span><span><span class="hljs-variable">$position</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strpos</span></span><span>(</span><span><span class="hljs-variable">$haystack</span></span>, </span><span><span class="hljs-variable">$needle</span></span><span>);
<p></span>echo $position;  // outputs 7<br>
</span>

In the example above, strpos returns 7, indicating the position where the string "world" first appears in "Hello, world!".

2. Introduction to strrpos

Similar to strpos, the strrpos function is used to find the position of the last occurrence of a substring within a main string. It returns the starting position of the last occurrence of the substring. If the substring is not found, it returns false.

<span><span><span class="hljs-variable">$haystack</span></span><span> = </span><span><span class="hljs-string">"Hello, world, world!"</span></span><span>;
</span><span><span class="hljs-variable">$needle</span></span><span> = </span><span><span class="hljs-string">"world"</span></span><span>;
</span><span><span class="hljs-variable">$position</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strrpos</span></span><span>(</span><span><span class="hljs-variable">$haystack</span></span>, </span><span><span class="hljs-variable">$needle</span></span><span>);
<p></span>echo $position;  // outputs 14<br>
</span>

In this example, strrpos returns 14 because the last occurrence of "world" in "Hello, world, world!" starts at index 14.

3. Differences Between strpos and strrpos

From the examples above, the main difference between strpos and strrpos is:

  • strpos: Returns the position of the substring's first occurrence.

  • strrpos: Returns the position of the substring's last occurrence.

In other words, strpos searches from left to right, while strrpos searches from right to left. Other behaviors of the two functions are essentially the same, including return types and parameters passed.

4. When to Use strpos and strrpos

When choosing between strpos and strrpos, the decision should be based on your actual needs:

Scenarios for Using strpos:

  • Finding the first occurrence: If you need to find the first occurrence of a substring in a string (for example, when processing user input to find the first valid match), then strpos is the preferred choice.

  • Performance considerations: When only the first occurrence matters, strpos is more efficient because it only traverses the string once.

Scenarios for Using strrpos:

  • Finding the last occurrence: When you need to find the last occurrence of a substring, strrpos is the ideal choice. For example, in log analysis, you might only be interested in the last log entry or the last error message.

  • Finding the last delimiter position: For instance, when parsing file paths, you may only care about the file extension. strrpos helps you quickly locate the last dot (.) position to extract the file extension.

5. Summary

  • strpos is used to find the first occurrence of a substring and is suitable for left-to-right searching scenarios.

  • strrpos is used to find the last occurrence of a substring and is suitable for right-to-left searching scenarios.

Choosing between these two functions should be based on specific business needs. If you want to find the first match in a string, use strpos; if you want the last match, use strrpos. Understanding their differences can help us make more precise decisions when writing PHP programs, improving code readability and efficiency.