Subjects

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

What are PHP superglobals? PHP सुपरग्लोबल्स क्या हैं?

Answer

Short Answer

When iterating over an associative array using a foreach loop, you use the $key => $value syntax. This allows you to access both the associative key (e.g., the string name) and its corresponding value simultaneously.

Detailed Explanation & Code Example

An associative array uses named keys that you assign to them rather than numeric indexes. A standard foreach loop can easily extract both.


$user = [
    "first_name" => "Prepiq",
    "role"       => "Admin",
    "status"     => "Active"
];

// Iterating over the array, capturing both key and value
foreach ($user as $key => $value) {
    // ucfirst() capitalizes the first letter of the key for display
    echo ucfirst(str_replace('_', ' ', $key)) . ": " . $value . "\n";
}

Expected Output:

First name: Prepiq
Role: Admin
Status: Active

सुपरग्लोबल्स बिल्ट-इन एरेज़ हैं जो हर स्कोप में अपने आप उपलब्ध होते हैं (global कीवर्ड की ज़रूरत नहीं)।

सुपरग्लोबलउद्देश्य
$_GETURL क्वेरी पैरामीटर्स
$_POSTPOST से भेजा गया फॉर्म डेटा
$_SESSIONसेशन में सुरक्षित डेटा
$_COOKIEक्लाइंट-साइड कुकीज़
$_SERVERसर्वर व रिक्वेस्ट हेडर्स की जानकारी
$_FILESअपलोड की गई फाइल का डेटा
$_REQUESTGET + POST + COOKIE combined
$_ENVएनवायरनमेंट वेरिएबल्स

इंटरव्यू टिप: बताएं कि SQL injection या XSS से बचने के लिए सुपरग्लोबल की raw वैल्यू हमेशा sanitize/validate करनी चाहिए।

Was this answer clear?