Problems checking out Empty()

Asked

Viewed 90 times

0

I’m having trouble checking a string using EMPTY(), it’s as follows. If I throw space into it " " it ceases to be empty.. and ends up inserting in the database the empty value, how can I solve this, which function?

1 answer

3


Remove the spaces around the value before applying the empty:

$valor = "   ";
$valor = trim($valor);
if(empty($valor)) {
    echo "Sim, está vazio";
}

https://ideone.com/Afl1nm

To function trim removes the following "blank" characters from the two ends of the string:

  • " " (ASCII 32 (0x20)), normal space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13(0x0D)), a car reset.
  • "\0" (ASCII 0 (0x00)), the NULL byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tabulation.
  • I didn’t know this function existed, it helped me a lot. Thank you!

  • 1

    I am glad you helped. Attention to the last clarification I have just made: the function trim affects only ends of strings, spaces in the middle are not removed.

  • But think to me, if there is space in the middle it means that the string is no longer empty, ie the functions fulfilled with their verification duties, correct?

  • Yes @user3163662. It is the role of the function trim it’s not leaving empty, it’s cleaning the ends.

  • But, there is some way even making these user checks to circumvent it and fill in empty?

  • @user3163662 Yes, there are certain Unic characters that this function does not address.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.