PHP has stored some variables which they use as superglobals variables. It is accessible from any function or the script. They are not bound by the rules.
PHP $GLOBALS
The $GLOBALS is a php super global variable can access from anywhere in the php script. To use it you have to use $GLOBAL[index] statement to run the super global variable. The word index holds the variable name.
Example
[php]<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS[‘z’] = $GLOBALS[‘x’] + $GLOBALS[‘y’];
}
addition();
echo $z;
?>[/php]
In the above case, we can see the variables are mentioned outside of the function statement. By using super global variable we can get the value of the $x and $y in the index and run the script. Also, we are telling php script to make an addition by using addition() function.
We will get the following result
100
PHP $_SERVER
This superglobal variable is used to get certain server data from the hosting such as headers, paths and script location.
Example
[php]<?php
echo $_SERVER[‘PHP_SELF’];
echo "<br>";
echo $_SERVER[‘SERVER_NAME’];
echo "<br>";
echo $_SERVER[‘HTTP_HOST’];
echo "<br>";
echo $_SERVER[‘HTTP_REFERER’];
echo "<br>";
echo $_SERVER[‘HTTP_USER_AGENT’];
echo "<br>";
echo $_SERVER[‘SCRIPT_NAME’];
?>[/php]
It collects the data from the server and shows in the browser. The result is depending on the server to server.
PHP $_REQUEST
The $_REQUEST superglobal variable is used to collect the data from the form field.
Example
[php]<form method="post" action="<?php echo $_SERVER[‘PHP_SELF’];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>[/php]
[php]<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = htmlspecialchars($_REQUEST[‘fname’]);
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>[/php]
In the above block of code first, we have created the HTML form to collect the information from the user. The field name names where the user can add his name. To get the information and present it in the browser we are using if statement to confirm the condition is met. In the condition we are using another global variable $_SERVER["REQUEST_METHOD"] at is mark as equal to “POST” in the condition. Then on the next line, we are verifying the user has put the right value on it. We are using “$name = htmlspecialchars($_REQUEST['fname']); ” statement where “htmlspecialchars” check the user has put the string details not the php code. Then we are running the if statement. If the condition is true will echo the result else it will show the error message.
PHP $_POST
The php $_POST is widely used in the php form to collect the data. Once the HTML form is submitted, it sent to the action php file to store the information.
Example
[php]<form method="post" action="<?php echo $_SERVER[‘PHP_SELF’];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>[/php]
[php]<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST[‘fname’];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>[/php]
In the POST instead of the request, we are using the POST global variable to get the form data. It works similar to the request variable.
PHP $_GET
Another global variable Get is also used to collect the data from the HTML form. The difference with the Get is the final output will be visible in the URL of the page.