PHP substr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, substr() returns a part of a string, starting at a position you choose.

Use a negative offset or length to count from the end of the string.

Syntax

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

PHP
$part = substr($text, 0, 5);
CallWhat it does
substr("Hello World", 0, 5)Returns "Hello".
substr("Hello World", 6)Returns "World".
substr("Hello World", -5)Returns "World", counting from the end.

Since PHP 8.0, substr() returns an empty string instead of false when the offset is out of range.

Library declarations
PHP
substr(string $string, int $offset, ?int $length = null): string

Parameters

ParameterDescription
stringRequired. The input string.
offsetRequired. Where to start. Positions begin at 0. A negative number counts from the end.
lengthOptional. How many characters to return. A negative number stops that many characters before the end. If omitted, returns to the end.

Return Value

Return ValueDescription
stringThe extracted part, or an empty string if nothing can be returned.

PHP Examples

Example 1 — Get Part of a String

Give a start position and a length to cut out a piece.

PHP
<?php
$text = "Hello World";

echo substr($text, 0, 5), PHP_EOL;
echo substr($text, 6), PHP_EOL;

How It Works

  1. Start at 0. The first character has position 0.
  2. Take 5 characters. The first call returns Hello.
  3. Skip the length. The second call starts at position 6 and returns the rest.
  4. Print both. Each result is shown on its own line.

Example 2 — Negative Values and Out-of-Range Offsets

Negative numbers count from the end. A start past the end gives an empty string.

PHP
<?php
$text = "Hello World";

echo substr($text, -5), PHP_EOL;
echo substr($text, 0, -6), PHP_EOL;
var_dump(substr("abc", 5));

How It Works

  1. Negative offset. -5 starts five characters from the end.
  2. Negative length. -6 drops the last six characters.
  3. Out of range. Offset 5 is past the end of a 3-character string.
  4. PHP 8 result. You get "", not false.

Example 3 — Shorten a Title with ...

Cut long text and add an ellipsis.

PHP
<?php
$title = "Learning PHP string functions";

if (strlen($title) > 15) {
    echo substr($title, 0, 15) . "...", PHP_EOL;
}

How It Works

  1. Measure first. strlen() returns 29, which is more than 15.
  2. Cut at 15. substr($title, 0, 15) keeps the first 15 characters.
  3. Add dots. The . operator joins the ellipsis.
  4. Tip. Use mb_substr() if the title may contain non-English characters.

Frequently Asked Questions

substr() returns a portion of a string. You give it a starting position and, optionally, a length.
string substr(string $string, int $offset, ?int $length = null). The offset starts at 0.
It counts from the end of the string. substr("Hello", -2) returns "lo".
Since PHP 8.0 it returns an empty string. Older versions returned false.
No. It works on bytes and can cut a multibyte character in half. Use mb_substr() for Unicode strings.

Next: substr_compare()

substr_compare() compares part of one string with another string.

substr_compare() 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