What is the difference between isset(), empty(), and is_null()? isset(), empty() और is_null() में क्या अंतर है?
Short Answer
Recursion is a programming technique where a function calls itself repeatedly until it reaches a specific "base case" that stops the loop. It is particularly useful for tasks involving nested structures, like traversing directories, parsing XML, or calculating factorials.
Detailed Explanation & Code Example
A recursive function must always have a base case. Without a base case, the function will call itself infinitely, resulting in a "Maximum function nesting level reached" Fatal Error.
Example: Calculating Factorial (5! = 5 * 4 * 3 * 2 * 1)
function calculateFactorial($number) {
// 1. The Base Case: Stop recursion when number is 1 or less
if ($number <= 1) {
return 1;
}
// 2. The Recursive Step: Function calls itself
return $number * calculateFactorial($number - 1);
}
echo calculateFactorial(5); // Output: 120
Common Mistakes
The most common mistake with recursion is forgetting the base case, or writing a base case that is never reached due to flawed logic. This causes a stack overflow error and crashes the script.
| फंक्शन | चेक करता है | true कब |
|---|---|---|
isset($v) | वेरिएबल है और null नहीं | किसी भी non-null वैल्यू के साथ सेट |
empty($v) | वेरिएबल "falsy" या unset | 0, "", null, false, "0", खाली array, या unset |
is_null($v) | वैल्यू strictly null है | सिर्फ अगर वैल्यू बिल्कुल null हो |
$a = 0;\nisset($a); // true\nempty($a); // true\nis_null($a); // false इंटरव्यू टिप: एक क्लासिक बग: empty() से फॉर्म फील्ड चेक करने पर सबमिट किया गया "0" गलती से empty माना जाएगा — जब 0 एक valid वैल्यू हो तो isset() के साथ explicit चेक इस्तेमाल करें।
Was this answer clear?