Certainly! PHP Superglobals are built-in global arrays in PHP that allow you to access various types of data such as input data from forms, server information, session data, and more. Here are the primary PHP Superglobals with explanations and examples:
1. `$GLOBALS`
- This array is used to access global variables from anywhere in a PHP script.
Example:
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z; // Outputs: 100
2. `$_SERVER` - This array contains information created by the web server such as headers, paths, and script locations.
Example (to get the name of the current script):
echo $_SERVER['PHP_SELF'];
3. `$_REQUEST` - Used to collect data after submitting an HTML form.
Example:
// After form submission, you can retrieve the value of the "username" field as:
$username = $_REQUEST['username'];
4. `$_POST` - Used to collect form data after submitting an HTML form with method="post". It's also used to pass variables.
Example:
$username = $_POST['username'];
5. `$_GET` - Used to collect form data after submitting an HTML form with method="get". It can also collect data sent in the URL.
Example:
// If the URL is http://example.com/index.php?name=John
$name = $_GET['name']; // Outputs: John
6. `$_FILES` - Used to collect file data after submitting an HTML form with enctype="multipart/form-data".
Example:
$file_name = $_FILES['myFile']['name'];
7. `$_ENV` - An array of environment variables.
Example:
echo $_ENV['USER']; // Might output the current system user, depending on server configuration.
8. `$_COOKIE` - Used to collect cookie values.
Example:
echo $_COOKIE['username']; // Will output the value of the 'username' cookie, if set.
9. `$_SESSION` - Allows you to store session data on the server, providing a way to preserve certain data across subsequent accesses.
Example:
session_start();
$_SESSION['username'] = 'JohnDoe';
echo $_SESSION['username']; // Outputs: JohnDoe
NOTE: Always remember to validate and sanitize data fetched from Superglobals before using it to prevent security vulnerabilities, especially when working with user input or when displaying data back to users to prevent attacks like SQL Injection or Cross-Site Scripting (XSS). In practice, for real-world applications, you might often want to use frameworks or libraries that handle many aspects of this data in more secure and standardized ways.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here