PHP Introduction | Functions | empty - Check Whether a Variable Is Empty
You can use empty to check whether a variable is empty. It is available from PHP 4.
Syntax
empty( $var )
Checks whether $var is empty. It returns TRUE if it is empty, and FALSE if it is not empty.
The following are considered empty.
""(empty string)0(integer 0)"0"(string 0)NULLFALSEarray()(empty array)var $var;(a variable declared without a value inside a class)
Example
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>Coding Factory</title>
<style>
p {
font-family: "Times New Roman";
font-style: italic;
font-size: 1.3em;
}
</style>
</head>
<body>
<?php
$var1 = NULL;
$var2 = "";
$var3 = "0";
$var4 = "Lorem";
if ( empty( $var1 ) ) {
echo "<p>var1 is empty.</p>";
} else {
echo "<p>var1 is not empty.</p>";
};
if ( empty( $var2 ) ) {
echo "<p>var2 is empty.</p>";
} else {
echo "<p>var2 is not empty.</p>";
};
if ( empty( $var3 ) ) {
echo "<p>var3 is empty.</p>";
} else {
echo "<p>var3 is not empty.</p>";
};
if ( empty( $var4 ) ) {
echo "<p>var4 is empty.</p>";
} else {
echo "<p>var4 is not empty.</p>";
};
?>
</body>
</html>