Content · Programming · PHP

PHP strripos() Function: Usage and Examples

Finds the last case-insensitive occurrence of a substring and returns its byte offset.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP strripos() finds the last case-insensitive occurrence of a substring and returns its byte offset. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strripos() in PHP?


Finds the last case-insensitive occurrence of a substring and returns its byte offset.

Its call structure is:

strripos($haystack, $needle, [$offset])

The arguments identify the values or resources passed to strripos(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.

Example 1: Basic Usage


This example introduces strripos with a direct call and a small, readable input.

<?php

var_dump(strripos("PHP php PHP", "php"));

Output:

int(8)

Finds the last case-insensitive occurrence of a substring and returns its byte offset. A result of zero is a match, not failure; compare with false strictly.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how strripos behaves.

<?php

var_dump(strripos("abc", "ABC") === 0);

Output:

bool(true)

The changed input illustrates why output may differ with the chosen values or environment. A result of zero is a match, not failure; compare with false strictly.

Practical Example: Using the Feature


The following example incorporates strripos into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$text = "one error and another ERROR"; $position = strripos($text, "error"); if ($position !== false) { echo substr($text, $position); }

Output:

ERROR

In a real application, check each operation result, validate values before use and clean up acquired resources. A result of zero is a match, not failure; compare with false strictly.

Important Behavior and Edge Cases


  • Purpose: Finds the last case-insensitive occurrence of a substring and returns its byte offset.
  • Key limitation: A result of zero is a match, not failure; compare with false strictly.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
strripos()Finds the last case-insensitive occurrence of a substring and returns its byte offset.
strrpos()Returns a byte position of the final occurrence, not the suffix string.

Common Beginner Mistakes


MistakeCorrect Approach
A result of zero is a match, not failure; compare with false strictly.The correct approach depends on this behavior: A result of zero is a match, not failure; compare with false strictly.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Finds the last case-insensitive occurrence of a substring and returns its byte offset.
  • A result of zero is a match, not failure; compare with false strictly.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strripos is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.