What is ltrim() in PHP?

The ltrim() function removes the leading space characters or other specified characters from the left side of a string.

The image below shows a visual representation of the ltrim() function.

Visual representation of ltrim() function

Syntax

ltrim(string,charsToBeRemoved)

Parameters

The ltrim() function takes two parameters:

  • A string from which characters need to be removed.
  • The list of characters that are to be removed from the left side of the string (optional).

If the value of the charsToBeRemoved is not specified, then the following characters are removed from the left side of the string:

  • " " - white space
  • “\t” - tab
  • “\n” - new line
  • “\x0B” - vertical tab
  • “\r” - carriage return
  • “\0” - NULL

Return value

ltrim() removes the leading space characters or the other specified characters from the left side of a string.

Code

The example below shows how to use ltrim() in PHP.

<?php
#single character removed from string
echo("ltrim('!educative','!'): ");
echo(ltrim('!educative','!'));
echo("\n");
echo("ltrim('!()educative','!'): ");
echo(ltrim('!()educative','!'));
echo("\n");
#multiple characters removed from string
echo("ltrim('!,()educative','! , ()'): ");
echo(ltrim('!,()educative','! , ()'));
echo("\n");
#default ~ without the list of characters to be removed
echo("ltrim(' educative'): ");
echo(ltrim(' educative'));
echo("\n");
?>

Free Resources