Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the input value is not empty spaces? [duplicate]

Tags:

php

I'm using PHP in my project and when any form is submitted I need to check if the input it not empty, so I use something like this:

if(empty($myVar)) {
    echo "Error";
} else {
    echo "success";
}

But this doesn't seem to be working as I want, so If the user enter empty space like " " so between the quotation is nothing but emptiness, but still PHP showing success message instead of error message.

I used isset($var) also, but its the samething, not working as I need.

What could be the problem? and How to solve it?

like image 667
Maohammed Hayder Avatar asked Nov 25 '25 12:11

Maohammed Hayder


1 Answers

Use trim function, which removes spaces in the beginning and in the end of a string:

if(empty(trim($myVar))) {
    echo "Error"
} else {
    echo "success";
}
like image 61
u_mulder Avatar answered Nov 28 '25 01:11

u_mulder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!