What is rtrim() in PHP?

The rtrim() function removes the trailing space characters or other specified characters from the right side of a string.

The following illustration shows a visual representation of the rtrim() function.

A visual representation of the rtrim() function

Syntax

rtrim(string,charsToBeRemoved)

Parameters

The rtrim() function takes two parameters:

  • A string from which characters need to be removed.
  • The list of characters to be removed from the right side of a string (optional).

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

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

Return value

rtrim()returns the modified string.

Code

The example below demonstrates how to use rtrim() in PHP:

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

Free Resources