# Get or Remove the Last Char in Strings in PHP

PHP function substr() allows manipulating a string for the rear. The function is defined as below.

string substr ( string $string , int$start [, int \$length ] )

It returns the portion of string specified by the start and length parameters.

• If start is negative, the starting position is counted from the end of the string.
• If length is given and positive, the returned string starts from the starting position but with at most the given length of characters.
• If length is given and negative, the returned string starts from the starting position, but with the given length of characters omitted from the end.

This allows us to return the last character or remove the last character.

substr("theory", -1);       // returns "y"
substr("theory", 0, -1);    // returns "theor"

substr("theory", -2);       // returns "ry"
substr("theory", 0, -2);       // returns "theo"

substr("theory", 0, 2);       // returns "th"
substr("theory", 2);       // returns "eory"