PHP trim

What is trim  in PHP?

trim() function in PHP  removes whitespace  from both sides of a string. trim() function also removes other predefined characters from the string. Genrally we use trim function where we require zero error like in the case of user id or email or password in which a whitespace can change the whole thing.There are two other function with some where Works like Trim function there are

  • ltrim()
  • rtrim()

Trim function can use to eliminate desired string or charter in PHP string  for example

Here $str is a PHP variable which stores string  .

<?php
$str = “whatwhyhow”;
echo trim($str,”w”);
?>

Output will be hathyow

What is LTRIM?

ltrim() works like trim function the difference is ltrim removes only whitespace from left side of string. there is no other difference between TRIM and LTRIM function .

 

What is RTRIM?

rtrim() works like trim function the difference is rtrim removes only whitespace from right side of string. there is no other difference between TRIM and RTRIM function .