Skip to content
PHP Logo

What PHP function is recommended to use in order to handle potential whitespace at the end of a line?

When dealing with potential whitespace at the end of a line in PHP, it is recommended to use the trim() function. This function will remove any whitespace characters (such as spaces, tabs, or newlines) from the beginning and end of a string. By using trim(), you can ensure that any trailing whitespace is removed before further processing the string.

php
// Example code snippet using trim() function to handle potential whitespace at the end of a line
$line = "Hello World      \n"; // String with whitespace at the end
$trimmedLine = trim($line); // Remove trailing whitespace
echo $trimmedLine; // Output: "Hello World"