PHP super globa...LOGIN

PHP super global variables

PHP Super Global Variables

Super global variables are enabled after PHP 4.1.0. They are variables that come with the PHP system and are available in all scopes of a script. .

PHP Super Global Variables

Several super global variables (superglobals) are predefined in PHP, which means that they are available in the entire scope of a script. You can use it in functions and classes without special instructions.

PHP Super Global Variable List:

·               $GLOBALS

## ·                   $_SERVER

·                 $_REQUEST

·                  

#·                  $_GET

·                $_FILES

·                                                                                                       In this chapter, we will explain several commonly used super global variables, and we will introduce the remaining variables in the next few chapters.

PHP $GLOBALS

$GLOBALS is a super global variable group of PHP that can be accessed in the entire scope of a PHP script.

$GLOBALS is a global combined array containing all variables. The name of the variable is the key of the array.

The following example introduces how to use the super global variable $GLOBALS:Example

<?php 
 $x = 75; 
 $y = 25;
  
 function addition() 
 { 
 $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
 }
  
 addition(); 
 echo $z; 
 ?>

In the above example, z is a super global variable in the $GLOBALS array, which is the same Can be accessed outside the function.

PHP $_SERVER

$_SERVER is a server that contains information such as header, path, script locations, etc. array. The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.

The following example shows how to use the elements in $_SERVER:

Example

<?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'];
 ?>
The following table lists all important elements in the $_SERVER variable:

QQ截图20161010142151.png

PHP $_REQUEST

QQ截图20161010142202.png

PHP $_REQUEST is used for Collect data submitted by HTML forms.

The following example shows a form with an input field (input) and a submit button (submit). When a user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to handle the form data. If you want another PHP file to handle this data, you can modify the specified script file name. Then, we can use the super global variable $_REQUEST to collect the input field data in the form:

Example

<html>
 <body>
 
 <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
 Name: <input type="text" name="fname">
 <input type="submit">
 </form>
 
 <?php 
 $name = $_REQUEST['fname']; 
 echo $name; 
 ?>
 
 </body>
 </html>

PHP $_POST

PHP $_POST is widely used to collect form data. Specify this attribute in the HTML form tag: "method="post".

The following example shows an input field (input) and submit button (submit) form. When the user submits form data by clicking the "Submit" button, the form data will be sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to. Process the form data. If you want other PHP files to process the data, you can modify the specified script file name. Then, we can use the super global variable $_POST to collect the input field data in the form:

Example

<html>
 <body>
 
 <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
 Name: <input type="text" name="fname">
 <input type="submit">
 </form>
 
 <?php 
 $name = $_POST['fname']; 
 echo $name; 
 ?>
 
 </body>
 </html>

PHP $_GET

PHP $_GET is also widely used to collect form data, specifying this attribute in the HTML form tag :"method="get".

$_GET can also collect data sent in the URL.

Suppose we have a hyperlinked HTML page containing parameters:

<html>
 <body>
 
 <a href="test_get.php?subject=PHP&web=php.cn">Test $GET</a>
 
 </body>
 </html>

When the user clicks on the link "Test $GET", the parameters "subject" and "web" will be sent to "test_get.php ",You can use the $_GET variable in the "test_get.php" file to get this data.

The following example shows the code of the "test_get.php" file:

Example

<html>
 <body>
 
 <?php 
 echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
 ?>
 
 </body>
 </html>
Next Section
<?php $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } addition(); echo $z; ?>
submitReset Code
ChapterCourseware