How to check whether a variable is empty in PHP

8 Answers

0 votes
$var = '';

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = 0;

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = null;

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = false;

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = true;

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

not empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = "php";

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

not empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = array();

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

empty

*/

 



answered Jun 18, 2022 by avibootz
0 votes
$var = array("php", "c", "c++", "java");

if (empty($var)) {
    echo "empty";
}
else {
    echo "not empty";
}




/*
run:

not empty

*/

 



answered Jun 18, 2022 by avibootz

Related questions

3 answers 240 views
1 answer 158 views
3 answers 244 views
3 answers 278 views
2 answers 278 views
1 answer 213 views
4 answers 241 views
...