I have an input box that with php will echo out names typed in. The problem is that when the user presses space it will echo out the blank character. I have searched and didnt find an absolute answer. I know that using !empty if there is absoutely anthing in the input field but if there is a space is null supposed to work.
How to avoid getting echo if there is blank space in input?
if (!empty($name['name']) || null ) {
echo 'Your name is '.$name;
}
{
//do nothing
}
Calls to the function
empty()will be true if it is unset, null, empty string, boolean false, or 0. Still I like avoiding heavy logic in conditionals… pull out the trimming and prepping the string before testing.To appease the fanatics in advance, I have to also add that you should escape any user input before printing it. That way you protect against XSS. To be safer add try this:
However, a side note… the
htmlentities()call does not protect against SQL injection, so if this data is going into a db you’ll have to do more work–since your question doesn’t indicate you’re doing anything else than printing out to screen, we can hold off the SQL injection discussion for another day.