trim()在此验证检查中实际做了什么?

I'm reading a PHP book, what does the below phrase mean?

To eliminate leading and trailing whitespace, we use the trim() function.

It's being used in the following condition check:

if(!empty(trim($_POST['username'])) && !empty(trim($_POST['email'])))
{
}

empty is checking to make sure the username and email variable is not empty

! signifies NOT

Here is what empty does http://php.net/manual/en/function.empty.php

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

trim

removes all whitespace from the beginning and end of the stringStrip whitespace (or other characters) from the beginning and end of a string

http://php.net/manual/en/function.trim.php

So if you have a string " user " trim(" user ") returns "user"

The trim() function is used to remove blank spaces in a string. For example:

$str = ' abc ';
echo trim($str);
// output will be: abc

So, as you can see, it remove spaces from the beginning and the end of the string. You can use ltrim() to remove spaces from the left or rtrim() to remove spaces from the right.

Hope that can help.

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string. Please visit this url:- http://php.net/manual/en/function.trim.php

I hope by visit this link you can learn more about trim function.