PHP strchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strchr() finds the first match of a substring and returns the text from there to the end.

It is an alias of strstr(), so both functions work the same way.

Syntax

Call strchr() with the values you need. Start with this line.

PHP
$rest = strchr($email, "@");
CallWhat it does
strchr("[email protected]", "@")Returns "@example.com".
strchr("[email protected]", "@", true)Returns "user".

The search is case-sensitive. Use stristr() for a case-insensitive search.

Library declarations
PHP
strchr(string $haystack, string $needle, bool $before_needle = false): string|false

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to look for.
before_needleOptional. If true, returns the part before the first match. Default is false.

Return Value

Return ValueDescription
stringThe part of the string from the first match onward, or the part before it when before_needle is true.
falseReturned when the needle is not found.

PHP Examples

Example 1 — Get Text from the First Match

Split an email at the @ sign.

PHP
<?php
$email = "[email protected]";

echo strchr($email, "@"), PHP_EOL;
echo strchr($email, "@", true), PHP_EOL;

How It Works

  1. Search for @. It is found at position 4.
  2. Default result. You get the match and everything after it.
  3. Add true. You get only the part before the match.
  4. Same as strstr(). strchr() is just another name.

Example 2 — Not Found and Case Matters

A missing needle returns false.

PHP
<?php
var_dump(strchr("Hello", "h"));
var_dump(strchr("Hello", "l"));

How It Works

  1. Search for lowercase h. The string has an uppercase H.
  2. No match. The search is case-sensitive, so it returns false.
  3. Search for l. The first l is found.
  4. Get the tail. The result is llo.

Example 3 — First Dot vs Last Dot

strchr() stops at the first match, so file names with many dots need care.

PHP
<?php
$file = "archive.tar.gz";

echo strchr($file, "."), PHP_EOL;
echo strrchr($file, "."), PHP_EOL;

How It Works

  1. Use strchr(). It finds the first dot.
  2. Get a long tail. The result is .tar.gz.
  3. Use strrchr(). It finds the last dot instead.
  4. Pick the right one. Use strrchr() for a simple file extension.

Frequently Asked Questions

strchr() finds the first occurrence of a substring and returns the text from that point to the end of the string. It returns false if there is no match.
strchr(string $haystack, string $needle, bool $before_needle = false): string|false. The last argument is optional.
There is no difference. strchr() is an alias of strstr(). Both behave exactly the same.
Yes. "Hello" does not match "h". Use stristr() if you need a case-insensitive search.
Pass true as the third argument. For example, strchr("[email protected]", "@", true) returns "a".

Next: strcmp()

strcmp() compares two strings and tells you which one is smaller, or if they are equal.

strcmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful