PHP substr() Function is used to return a part of a string. PHP substr() function is PHP built-in function.
Syntax for substr:
substr(string $string, int $start, int $length) ;
$string : Required. It is input string to extract a part of string.
$start : Required. It is position where to begin in the string.
- A positive number - Returned string will start at a specified position in the string.
- A negative number - Returned string will start at a specified position from the end of the string.
- 0 - Returned string will start at the first character in string.
$length : Optional. It is the length of the returned string. Default is to the end of the string.
- A positive number - The length to be returned from the start parameter.
- A Negative number - The length to be returned from the end of the string.
- If the length parameter is 0, NULL, or FALSE - it return an empty string.
Return Values for substr:
The substr() function returns the extracted part of the string called sub string. It also returns FALSE on failure or an empty string.
Let's see below example to understand php substr() Function in details.