How to check whether a variable is empty in PHP
In this post we will give you information about How to check whether a variable is empty in PHP. Hear we will give you detail about How to check whether a variable is empty in PHPAnd how to use it also give you demo for it if it is necessary.
Use the PHP empty()
function
You can use the PHP empty()
function to find out whether a variable is empty or not. A variable is considered empty if it does not exist or if its value equals FALSE
.
Let’s try out the following example to understand how this function basically works:
<?php
$var1 = '';
$var2 = 0;
$var3 = NULL;
$var4 = FALSE;
$var5 = array();
// Testing the variables
if(empty($var1)){
echo 'This line is printed, because the $var1 is empty.';
}
echo "<br>";
if(empty($var2)){
echo 'This line is printed, because the $var2 is empty.';
}
echo "<br>";
if(empty($var3)){
echo 'This line is printed, because the $var3 is empty.';
}
echo "<br>";
if(empty($var4)){
echo 'This line is printed, because the $var4 is empty.';
}
echo "<br>";
if(empty($var5)){
echo 'This line is printed, because the $var5 is empty.';
}
?>
i hope you like this small article.
Hope this code and post will helped you for implement How to check whether a variable is empty in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs