``` --- ## Definition and Usage"> ``` --- ## Definition and Usage" />
Easy Tutorial
❮ Func Mysqli Fetch Object Php Is_Callable Function ❯

PHP substr() Function

PHP String Reference Manual

Example

Return "world" from the string:

<?php
echo substr("Hello world", 6);
?>

Definition and Usage

The substr() function returns a part of a string.

Note: If the start parameter is negative and length is less than or equal to start, length becomes 0.


Syntax

Parameter Description
string Required. Specifies the string to return a part of.
start Required. Specifies where to start in the string. Positive - Starts at the specified position in the string. Negative - Starts at the specified position from the end of the string. 0 - Starts at the first character in the string.
length Optional. Specifies the length of the returned string. Default is to the end of the string. Positive - Returns the specified number of characters from the start. Negative - Returns from the end of the string.

Technical Details

Return Value: Returns the extracted part of the string, or FALSE on failure, or an empty string.
PHP Version: 4+
--- ---
Changelog: In versions PHP 5.2.2 to 5.2.6, if the start parameter indicates a negative truncation or out-of-bounds position, FALSE is returned. Other versions return the string starting from the start position.
--- ---

More Examples

Example 1

Using the start parameter with different positive and negative numbers:

<?php
echo substr("Hello world", 10) . "<br>";
echo substr("Hello world", 1) . "<br>";
echo substr("Hello world", 3) . "<br>";
echo substr("Hello world", 7) . "<br>";

echo substr("Hello world", -1) . "<br>";
echo substr("Hello world", -10) . "<br>";
echo substr("Hello world", -8) . "<br>";
echo substr("Hello world", -4) . "<br>";
?>

Example 2

Using the start and length parameters with different positive and negative numbers:

<?php
echo substr("Hello world", 0, 10) . "<br>";
echo substr("Hello world", 1, 8) . "<br>";
echo substr("Hello world", 0, 5) . "<br>";
echo substr("Hello world", 6, 6) . "<br>";

echo substr("Hello world", 0, -1) . "<br>";
echo substr("Hello world", -10, -2) . "<br>";
echo substr("Hello world", 0, -6) . "<br>";
echo substr("Hello world", -2, -3) . "<br>";
?>

❮ Func Mysqli Fetch Object Php Is_Callable Function ❯