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 कीवर्ड की ज़रूरत नहीं)।
| सुपरग्लोबल | उद्देश्य |
|---|---|
$_GET | URL क्वेरी पैरामीटर्स |
$_POST | POST से भेजा गया फॉर्म डेटा |
$_SESSION | सेशन में सुरक्षित डेटा |
$_COOKIE | क्लाइंट-साइड कुकीज़ |
$_SERVER | सर्वर व रिक्वेस्ट हेडर्स की जानकारी |
$_FILES | अपलोड की गई फाइल का डेटा |
$_REQUEST | GET + POST + COOKIE combined |
$_ENV | एनवायरनमेंट वेरिएबल्स |
इंटरव्यू टिप: बताएं कि SQL injection या XSS से बचने के लिए सुपरग्लोबल की raw वैल्यू हमेशा sanitize/validate करनी चाहिए।
Was this answer clear?