Content · Programming · PHP

PHP strchr() Function: Usage and Examples

Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle.

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

PHP strchr() alias of strstr() that returns the portion of a string starting at the first occurrence of a needle. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strchr() in PHP?


Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle.

Its call structure is:

strchr($haystack, $needle, [$before_needle])

The arguments identify the values or resources passed to strchr(). 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 strchr with a direct call and a small, readable input.

<?php

echo strchr("invoice.pdf", ".");

Output:

.pdf

Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle. A missing needle returns false; strpos() is preferable when only the position is needed.

Example 2: Another Behavior or Input


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

<?php

var_dump(strchr("readme", ".") === false);

Output:

bool(true)

The changed input illustrates why output may differ with the chosen values or environment. A missing needle returns false; strpos() is preferable when only the position is needed.

Practical Example: Using the Feature


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

<?php

$address = "[email protected]"; $domain = strchr($address, "@"); if ($domain !== false) { echo substr($domain, 1); }

Output:

example.test

In a real application, check each operation result, validate values before use and clean up acquired resources. A missing needle returns false; strpos() is preferable when only the position is needed.

Important Behavior and Edge Cases


  • Purpose: Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle.
  • Key limitation: A missing needle returns false; strpos() is preferable when only the position is needed.
  • 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
strchr()Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle.
strstr()Returns the substring from the first match, rather than a numeric position.

Common Beginner Mistakes


MistakeCorrect Approach
A missing needle returns false; strpos() is preferable when only the position is needed.The correct approach depends on this behavior: A missing needle returns false; strpos() is preferable when only the position is needed.
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


  • Alias of strstr() that returns the portion of a string starting at the first occurrence of a needle.
  • A missing needle returns false; strpos() is preferable when only the position is needed.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strchr 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.