Skip to content
PHP Logo

What function can be used to remove leading and trailing whitespace from a string in PHP?

Leading and trailing whitespace in a string can be removed in PHP using the `trim()` function. This function takes a string as input and returns a new string with any leading or trailing whitespace removed. This is useful for cleaning up user input or data retrieved from external sources before further processing.

php
$string = "   Hello, World!   ";
$trimmedString = trim($string);

echo $trimmedString; // Output: "Hello, World!"