Check Variables with isset() empty()
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
Integer 42 is set and non-empty
Empty string is empty
Non-empty string is set
Zero is considered empty in PHP
Null is unset
Algorithm Flow

Solution Approach
Best Answers
<?php
function solution($val) {
if (!isset($val) || $val === null) return 'unset';
if (empty($val)) return 'empty';
return 'isset';
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
