Code Logo

Check Variables with isset() empty()

Published at25 Jul 2026
PHP Functions Easy 0 views
Like0

Write a PHP function that takes a value and returns "isset" if the value is set and not null, "empty" if the value is set but empty (0, "", null, false, []), or "unset" if the variable is not set (null counts as unset here). Use isset() and empty() built-in functions.

isset() is a PHP language construct that checks if a variable is set and is not NULL. empty() checks if a variable is set and is considered "empty" (empty string, 0, "0", null, false, []). These are fundamental PHP functions for safe variable handling.

Unlike many languages where accessing undefined variables causes errors, PHP's isset() and empty() provide safe checking. They are used extensively in PHP applications for form validation, array key checking, and configuration defaults.

Time complexity is O(1). Both isset() and empty() are language constructs, not functions, making them very fast.

Example Input & Output

Example 1
Input
42
Output
isset
Explanation

Integer 42 is set and non-empty

Example 2
Input
""
Output
empty
Explanation

Empty string is empty

Example 3
Input
"hello"
Output
isset
Explanation

Non-empty string is set

Example 4
Input
0
Output
empty
Explanation

Zero is considered empty in PHP

Example 5
Input
null
Output
unset
Explanation

Null is unset

Algorithm Flow

Recommendation Algorithm Flow for Check Variables with isset() empty()
Recommendation Algorithm Flow for Check Variables with isset() empty()

Solution Approach

function solution($val) {
    if (!isset($val) || $val === null) return 'unset';
    if (empty($val)) return 'empty';
    return 'isset';
}

Best Answers

php - Approach 1
<?php
function solution($val) {
    if (!isset($val) || $val === null) return 'unset';
    if (empty($val)) return 'empty';
    return 'isset';
}