Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 5 of 13 · PHP Basics & Syntax
Interview question

Explain variable scope in PHP (local, global, static). PHP में वेरिएबल स्कोप समझाएं (local, global, static)।

Answer

Short Answer

break immediately exits the entire loop (or switch statement). continue skips the rest of the current loop iteration and moves directly to the next iteration.

Detailed Explanation & Examples

Example of break: Stop searching as soon as you find what you need.


for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        break; // Exits the loop completely
    }
    echo $i . " ";
}
// Output: 1 2

Example of continue: Skip a specific condition but keep looping.


for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue; // Skips printing '3', moves to '4'
    }
    echo $i . " ";
}
// Output: 1 2 4 5

When to use

  • Use break when continuing the loop is pointless or dangerous (e.g., finding the correct user record in an array).
  • Use continue when you want to filter out specific items in a loop but process the rest (e.g., skipping inactive users in a mailing list).
स्कोपविवरण
Localफंक्शन के अंदर डिक्लेयर, सिर्फ उसी फंक्शन में एक्सेसिबल
Globalसभी फंक्शन्स के बाहर डिक्लेयर; फंक्शन के अंदर एक्सेस के लिए global कीवर्ड या $GLOBALS[] चाहिए
Staticstatic कीवर्ड से डिक्लेयर; फंक्शन कॉल्स के बीच वैल्यू बनाए रखता है
$x = 10; // global\nfunction test() {\n    global $x;\n    static $count = 0;\n    $count++;\n    echo $x, $count;\n}

इंटरव्यू टिप: बताएं कि PHP फंक्शन्स को डिफ़ॉल्ट रूप से ग्लोबल वेरिएबल्स का एक्सेस नहीं होता — JavaScript से आने वाले डेवलपर्स के लिए यह एक आम गलतफहमी है।

Was this answer clear?