PHP trim() Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The trim() function in PHP is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right. Related Functions: PHP | ltrim() Function PHP | rtrim() Function Syntax: trim($string, $charlist) Parameters:The function accepts one mandatory parameter and one optional parameter as shown in the above syntax and described below. $string: This parameter specifies the string from which the whitespace and predefined characters from left and right are to be removed $charlist: This is an optional parameter which specifies the characters that are to be removed from the string. If this is not mentioned then all of the following characters will be removed: "\0" - NULL "\t" - tab "\n" - new line "\x0B" - vertical tab "\r" - carriage return " " - ordinary white space Return Value: It returns the modified string by removing the whitespace and also the predefined characters from both sides of a string that is left and right. Below programs illustrate the trim() function: Program 1: php <?php // PHP program to demonstrate the use of trim() // function when second parameter is present // removes the predefined characters from // front and back $str = "Hello World!"; echo trim($str, "Hell!"); ?> Output: o World Program 2: php <?php // PHP program to demonstrate the use of trim() // function when second parameter is absent $str = " geeks for geeks "; // removes all leading and trailing whitespaces echo trim($str); ?> Output: geeks for geeks Reference: https://www.php.net/manual/en/function.trim.php Create Quiz Comment T Twinkl Bajaj Follow 0 Improve T Twinkl Bajaj Follow 0 Improve Article Tags : Misc Web Technologies PHP PHP-string Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like