Home PHP What is difference between isset() and empty()

What is difference between isset() and empty()

Author

Date

Category

In PHP, isset and empty are used to check whether a variable has a value or not, but they have different definitions and behaviors.

isset() is a function that returns true if the variable exists and is not null. It checks whether a variable has been set or not. If the variable exists and has any value other than null, isset() returns true. For example:

$var = "Hello World!";
if (isset($var)) {
    echo "The variable is set.";
}

In the above example, since $var is set to a string value, isset($var) returns true, and the message “The variable is set.” will be printed.

On the other hand, empty() is a function that returns true if the variable is empty or not set at all. It checks whether a variable has been set and has a non-empty value or not. If the variable does not exist, or if its value is null, an empty string, an empty array, false, or a zero integer, empty() returns true. For example:

$var = "";
if (empty($var)) {
    echo "The variable is empty.";
}

In the above example, since $var is set to an empty string, empty($var) returns true, and the message “The variable is empty.” will be printed.

So, the main difference between isset() and empty() is that isset() checks whether a variable is set or not, while empty() checks whether a variable is set and has a non-empty value or not.

Previous articleWhat is WordPress
Next articleWhat are MX records?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subhash Shipu

PHP Expert

Hey there! I'm a PHP geek on a mission to blog my way through the coding chaos. When I'm not chasing semicolons, I'm busy cuddling my pet Coco, who thinks debugging means chasing her own tail. Join the fun!

Subscribe

Recent posts