PHP - SPLessons

PHP Global Variables

Home > Lesson > Chapter 17
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Global Variables

PHP Global Variables

shape Description

Superglobal variables are the PHP Global variables, which can be accessed anywhere in the program, regardless of scope. These PHP Global Variables can be accessed in any function, class or file without declaring anything specially to those variables.

PHP GET and POST Methods

shape Description

GET or POST method helps to submit and transfer the data in the forms. Both are similar, but post method is very secure for the data submission in the form as it transfers the data in hidden format from page to page. Using get method, the data is carried out through the URL which is visible and is insecure when compared with post method.

Data Transfer through GET Method

The data transferred through GET Method will be stored in the superglobal variable $_GET.This data submitted can be accessed in the form using GET Method by $_GET global variable. [php] <html> <body> <form action="#" method="get"> Name : <input type="text" name="name" /> Email : <input type="text" name="email" /> Contact : <input type="text" name="contact" /> <input type="submit" name="submit" value="Submit"></input> </form> <?php if( $_GET["name"] || $_GET["email"] || $_GET["contact"]) { echo "Welcome: ". $_GET['name']. "<br />"; echo "Your Email is: ". $_GET["email"]. "<br />"; echo "Your Mobile No. is: ". $_GET["contact"]; } ?> </body> </html> [/php] Output:

Data Transfer through POST Method

The data transfer through POST Method will be stored in the superglobal variable $_POST.This data submitted in the form can be accessed using POST Method by $_POST global variable. [php] <html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="User-details"> Name : <input type="text" name="name" /> Mobile : <input type="tel" name="phone" /> <input type="submit" name="submit" value="Send" /> </form> <?php if ($_POST['submit'] == "Send") { $name = $_POST['name']; $mobile = $_POST['mobile']; if (empty($name)&& empty($mobile)) { echo "Name & Mobile Number is empty"; } else { echo $name; echo $mobile; } } ?> </body> </html> [/php] Output:

PHP $GLOBALS

shape Description

$GLOBALS is PHP superglobal variable, that is used to allow the usage of the global variables anywhere in the program including functions and classes. The PHP saves all the variables in an array called as $GLOBALS["varible_name"].

shape Example

[php] <html> <body> <?php $x = 30; $y = 40; function multiply() { $GLOBALS['z'] = $GLOBALS['x'] * $GLOBALS['y']; } multiply(); echo $z; ?> </body> </html> [/php] Output:

PHP $_REQUEST

shape Description

By using $_REQUEST Global variable, information submitted by HTML Form can be collected and information which is passed by using parameter passing method can be collected through url.

shape Example

[php] <html> <body> <form method="post" action="" name="User-details"> Name : <input type="text" name="name" /> Mobile : <input type="tel" name="phone" /> <input type="submit" name="submit" value="Send" /> </form> <?php if ($_POST['submit'] == "Send") { $name = $_REQUEST['name']; $mobile = $_REQUEST['mobile']; if (empty($name)&& empty($mobile)) { echo "Name & Mobile Number is empty"; } else { echo $name; echo $mobile; } } ?> </body> </html> [/php] Output:

PHP $_SERVER

shape Description

$_SERVER is a superglobal variable of PHP that holds the total data about the urls paths, script locations and about the headers.

shape Examples

[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] Output: Other superglobals variables $_FILES , $_ENV, $_COOKIE, $_SESSION can be discussed in further lessons.

Summary

shape Points

  • POST method is very secure.
  • Data is carried out through the URL using GET method.
  • To access the global variables $GLOBALS is used.
  • $_SERVER holds entire data about the url's paths, script locations and about the headers.