In PHP, a superglobal variable is a predefined variable that is always available in all scopes throughout a script. There are several superglobal variables in PHP, which are:
$GLOBALS: This is an array that contains all the global variables in your script. You can access a global variable using its name as a key in the $GLOBALS array.
$_SERVER: This is an array that contains information about the web server and the current script. It includes variables such as $_SERVER[‘PHP_SELF’], which contains the name of the current script, and $_SERVER[‘HTTP_HOST’], which contains the hostname of the server.
$_GET: This is an array that contains the variables passed to the current script via the HTTP GET method.
$_POST: This is an array that contains the variables passed to the current script via the HTTP POST method.
$_FILES: This is an array that contains the variables for uploaded files.
$_COOKIE: This is an array that contains the variables set by HTTP cookies.
$_SESSION: This is an array that contains the variables stored in a session.
$_REQUEST: This is an array that contains the variables passed to the current script via the GET, POST, and COOKIE methods.
$_ENV: This is an array that contains the variables set by the environment in which the current script is running.
$GLOBALS is a superglobal array in PHP that contains all the global variables in your script. It is an associative array where the keys are the names of the global variables, and the values are the values of the global variables.
You can access a global variable using its name as a key in the $GLOBALS array. For example, to access the value of the $x global variable, you would use $GLOBALS[‘x’].
Here is an example of how you can use the $GLOBALS array to access and modify global variables:
$x = 10;
$y = 20;
function sum() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
sum();
echo $z; // Outputs: 30
It is generally a good practice to avoid using global variables, as they can make your code more difficult to understand and maintain. Instead of using global variables, you can use function arguments or return values to pass data between functions and methods.
$_SERVER is a superglobal array in PHP that contains information about the web server and the current script. It is an associative array where the keys are the names of the variables, and the values are the values of the variables.
Here are a few common variables that you can find in the $_SERVER array:
$_SERVER[‘PHP_SELF’]: This variable contains the name of the current script, with the file extension.
$_SERVER[‘HTTP_HOST’]: This variable contains the hostname of the server.
$_SERVER[‘HTTP_USER_AGENT’]: This variable contains the user agent string of the client’s web browser.
$_SERVER[‘REMOTE_ADDR’]: This variable contains the IP address of the client.
$_SERVER[‘REQUEST_METHOD’]: This variable contains the HTTP method used to request the current script.
Here is an example of how you can use the $_SERVER array:
echo "The current script is: " . $_SERVER['PHP_SELF'] . "\n";
echo "The server hostname is: " . $_SERVER['HTTP_HOST'] . "\n";
echo "The user agent is: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
echo "The client's IP address is: " . $_SERVER['REMOTE_ADDR'] . "\n";
echo "The request method is: " . $_SERVER['REQUEST_METHOD'] . "\n";
$_GET is a superglobal array in PHP that contains the variables passed to the current script via the HTTP GET method. The GET method is used to request data from a server by encoding the data in the URL of the request.
The variables in the $_GET array are stored as key-value pairs, where the keys are the names of the variables, and the values are the values of the variables.
To pass variables to a script using the GET method, you can add a query string to the URL of the script. The query string consists of a series of key-value pairs, separated by the & character. Each key-value pair is in the form key=value.
Here is an example of how you can pass variables to a script using the GET method:
http://example.com/script.php?name=John&email=john@example.com
In the script, you can access the variables using the $_GET array. For example:
echo "Your name is: " . $_GET['name'] . "\n";
echo "Your email is: " . $_GET['email'] . "\n";
It is important to note that the GET method is not suitable for sending sensitive data, such as passwords or credit card numbers, as the data is visible in the URL and can be easily intercepted.
$_POST is a superglobal array in PHP that contains the variables passed to the current script via the HTTP POST method. The POST method is used to send data to a server to be processed, such as when submitting a form.
The variables in the $_POST array are stored as key-value pairs, where the keys are the names of the form elements, and the values are the values entered by the user.
To pass variables to a script using the POST method, you can use an HTML form with a method attribute set to “post”. The form should have input elements, such as text fields and buttons, with name attributes that correspond to the names of the variables you want to pass.
Here is an example of an HTML form that uses the POST method:
<form action="/script.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
In the script, you can access the variables using the $_POST array. For example:
echo "Your name is: " . $_POST['name'] . "\n";
echo "Your email is: " . $_POST['email'] . "\n";
It is important to note that the POST method is more secure than the GET method, as the data is not visible in the URL and is less likely to be intercepted.
$_FILES is a superglobal array in PHP that contains the variables for uploaded files. It is an associative array where the keys are the names of the file input elements in the HTML form, and the values are arrays that contain information about the uploaded files.
To upload a file using an HTML form, you can use an input element with a type attribute set to “file”. The form should also have a method attribute set to “post” and an enctype attribute set to “multipart/form-data”.
Here is an example of an HTML form that can be used to upload a file:
<form action="/script.php" method="post" enctype="multipart/form-data">
Select a file to upload: <input type="file" name="file"><br>
<input type="submit" value="Upload">
</form>
In the script, you can access the uploaded file using the $_FILES array. For example:
$file = $_FILES['file'];
echo "The uploaded file is: " . $file['name'] . "\n";
echo "The file type is: " . $file['type'] . "\n";
echo "The file size is: " . $file['size'] . " bytes\n";
It is important to note that the $_FILES array only contains information about uploaded files. It does not contain the actual file data. To access the file data, you can use the file_get_contents() function or the fopen() function.
$_COOKIE is a superglobal array in PHP that contains the variables set by HTTP cookies. A cookie is a small piece of data that is stored on the client’s computer by the web server, and can be retrieved by the server when the client makes subsequent requests.
To set a cookie in PHP, you can use the setcookie() function. This function takes a name, a value, and an expiration time as arguments, and sets a cookie on the client’s computer.
Here is an example of how you can set a cookie in PHP:
setcookie('name', 'John', time() + 86400);
This example sets a cookie named name with a value of “John” and an expiration time of one day.
To access a cookie in the $_COOKIE array, you can use the name of the cookie as a key. For example:
echo "Your name is: " . $_COOKIE['name'] . "\n";
It is important to note that the $_COOKIE array only contains information about cookies that have been set by the server. It does not contain information about cookies that have been set by the client, such as by using JavaScript.
$_SESSION is a superglobal array in PHP that contains the variables stored in a session. A session is a way to store data on the server for a particular user, and to retrieve the data when the user makes subsequent requests.
To use sessions in PHP, you must first start a session using the session_start() function. This function must be called at the beginning of your script, before any output is sent to the browser.
Here is an example of how you can start a session in PHP:
session_start();
To store a variable in a session, you can assign a value to a key in the $_SESSION array. For example:
$_SESSION['name'] = 'John';
To access a session variable, you can use the name of the variable as a key in the $_SESSION array. For example:
echo "Your name is: " . $_SESSION['name'] . "\n";
To end a session and delete all the session variables, you can use the session_destroy() function. This function removes all the data associated with the current session.
Here is an example of how you can end a session in PHP:
session_destroy();
$_REQUEST is a superglobal array in PHP that contains the variables passed to the current script via the GET, POST, and COOKIE methods. It is an associative array where the keys are the names of the variables, and the values are the values of the variables.
The $_REQUEST array is useful for accessing variables that are passed to the script using any of the above methods, without having to check which method was used.
For example, consider a script that processes a form submission. The form can be submitted using either the GET or POST method, and the script needs to access the form data regardless of the method used. Instead of using the $_GET and $_POST arrays separately, you can use the $_REQUEST array to access the form data.
Here is an example of how you can use the $_REQUEST array to access form data:
echo "Your name is: " . $_REQUEST['name'] . "\n";
echo "Your email is: " . $_REQUEST['email'] . "\n";
It is important to note that the $_REQUEST array is not recommended for security reasons, as it can potentially expose sensitive data, such as passwords or credit card numbers, if the script is not carefully written. Instead of using the $_REQUEST array, you should use the $_GET, $_POST, or $_COOKIE arrays depending on the method used to pass the variables.
$_ENV is a superglobal array in PHP that contains the variables set by the environment in which the current script is running. It is an associative array where the keys are the names of the variables, and the values are the values of the variables.
The $_ENV array is useful for accessing environment variables, which are variables that are set outside of your PHP scripts and are used to configure the runtime environment.
To set an environment variable in PHP, you can use the putenv() function. This function takes a string in the form “name=value” as an argument, and sets the environment variable with the given name to the given value.
Here is an example of how you can set an environment variable in PHP:
putenv("NAME=John");
To access an environment variable in the $_ENV array, you can use the name of the variable as a key. For example:
echo "Your name is: " . $_ENV['NAME'] . "\n";
It is important to note that the $_ENV array only contains variables that are set in the environment. It does not contain variables that are set within your PHP scripts, such as global or local variables.