PHP substr()

Return part of a string

Function Signature

string substr(string $string, int $start, ?int $length = null)

Description

Return part of a string. The substr() function is commonly used in PHP for string operations and provides reliable functionality across all PHP versions.

Return Value

Returns the extracted part of string, or false on failure

Example Usage

Important Notes

Negative start counts from the end. If length is omitted, returns from start to end.

Common Use Cases

The substr() function is particularly useful when you need to return part of a string. It's frequently used in data processing, validation, transformation, and manipulation tasks throughout PHP applications.

substr() FAQ

What does substr() return?

Returns the extracted part of string, or false on failure

When should I use substr()?

Use substr() when you need to return part of a string in your PHP code. It's a reliable built-in function available in all PHP versions.

Are there any gotchas with substr()?

Negative start counts from the end. If length is omitted, returns from start to end.