PHP superglobal...LOGIN

PHP superglobal 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 This means they are available in all scopes 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, in the entire PHP script can be accessed within the scope.

$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 an array containing information such as header, path, and script locations. 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:

##PHP $_REQUEST

##PHP $_REQUEST is used to 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 a file to process 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 a form with an input field and a submit button. When the user submits form data by clicking the "Submit" button, the form data will be sent to the action attribute in the <form> tag. The script file specified in. In this example, we specify the file to process the form data. If you want another PHP file to process the data, you can modify the specified script file name. Then, we can use the super global variable $. _POST to collect 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. Specify 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 parameter "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>


Tips: If you want to learn more To learn more about $_POST and $_GET, please visit our PHP Forms chapter.


Next Section

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