substr()

The substr() function returns a portion of a string specified by parameters for offset and length.

Syntax

substr($string, $offset, $length)

The $string parameter is the string from which a portion will be returned.

The $offset parameter is an int value and specifies the start of the substring in the following manner:

  • Positive or zero values specify the zero-based character position from the start of the string.
  • Negative values indicate the character position from the end of the string.
  • A value greater than the length of $string will return an empty string.

The $length parameter is an optional int that specifies the number of characters to return in the following manner:

  • Positive values specify to return that number of characters, or characters up to the end of $string if fewer than $length characters are left after $offset is determined.
  • A null or omitted value will return characters from $offset until the end of $string.
  • Negative values will return characters from $offset until $length characters from the end of $string.
  • A value of zero, or a negative value that specifies a position before $offset, will return an empty string.

Codebyte Example

The following example gives three examples of the substr() function operating on the same string.

Code
Output
Loading...

All contributors

Looking to contribute?

Learn PHP on Codecademy