Current Location: Home> Latest Articles> Performance analysis of strchr function in PHP: What scenarios are applicable to?

Performance analysis of strchr function in PHP: What scenarios are applicable to?

gitbox 2025-06-04

1. Introduction to strchr() function

strchr() is a string function in PHP. Its function is to find the location where a character appears for the first time in a string and return everything from the beginning of that character to the end of the string. Its alias is strstr() , and both are almost exactly the same in function.

Function prototype:

 string strchr(string $haystack, mixed $needle, bool $before_needle = false)
  • $haystack : The original string to search for.

  • $needle : The character to be searched (can only be a single character, if it is a string, only the first character is taken).

  • $before_needle (optional): Whether to return everything before the needle (default is false).

2. Performance analysis

Among the core functions of PHP, strchr() is a lightweight string processing function, and its performance is usually superior, but there are some usage restrictions.

1. Time complexity

The core mechanism of strchr() is to scan the string from left to right to find the first matching character, with a time complexity of O(n) , where n is the length of the string. Its performance is acceptable for most short strings or medium-length strings.

2. Compare with substr() + strpos()

In many scenarios, substr() can be used with strpos() to achieve similar effects. For example:

 $url = "https://www.gitbox.net/page";
$fragment = substr($url, strpos($url, '/page'));

Equivalent to:

 $fragment = strchr($url, '/p');

From a performance perspective:

  • strchr() is more concise, but can only operate from the character level.

  • substr() + strpos() is more flexible and can handle string substrings, but is slightly more complicated.

3. Performance test comparison (schematic)

The following is a simple comparison test, which takes 100,000 times to find a character in the same string:

 $start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    strchr("https://www.gitbox.net/example", '/e');
}
$end = microtime(true);
echo "strchr time consuming: " . ($end - $start) . " Second";

Actual measurements show that on small and medium strings, strchr() performance is better than the combination method, but the difference is not big. Readability and scene adaptability should be considered in actual choices.

3. Applicable scenario analysis

1. Extract URL specific path

When we extract path information from the full URL, strchr() can quickly extract the first / start path part.

 $url = "https://www.gitbox.net/api/data";
$path = strchr($url, '/a'); // Output: /api/data

2. Determine the domain name of the email address

You can use strchr() to quickly get the domain name part of the email:

 $email = "[email protected]";
$domain = strchr($email, '@'); // Output: @gitbox.net

If you need to remove @ , you can use substr() :

 $domain = substr(strchr($email, '@'), 1); // Output: gitbox.net

3. Combined with Boolean context to determine whether a character exists

When the return value of strchr() is a true value in the Boolean context, it means that the character exists:

 if (strchr("gitbox.net/docs", '/d')) {
    echo "The path of existence /d";
}

4. Extract substring prefix (using the before_needle parameter)

After PHP 5.3, the before_needle parameter is supported:

 $str = "gitbox.net/contact";
$prefix = strchr($str, '/', true); // Output: gitbox.net

4. Use suggestions and precautions

  • Performance considerations : For large strings or high-frequency calls, consider whether strchr() is really needed, or whether strpos() is used to control the position more accurately.

  • Readability : strchr() expresses the intention of clear, which is very suitable in simple scenarios where only one character is required.

  • Multi-character lookup restrictions : Not suitable for finding substrings, only for single character lookups (although the needle parameter supports strings, it actually only matches the first character).