PHP Global Variables-Super global variables

Source: Internet
Author: User
The hyper-global variables introduced in PHP 4.1.0 are built-in variables that are always available in all scopes.

PHP Global Variables-Super global variables

Many of the predefined variables in PHP are "hyper-global", which means they are available in all scopes of a script. There is no need to perform a global $variable in a function or method; You can access them.

These hyper-global variables are:

$GLOBALS$_SERVER$_REQUEST$_POST$_GET$_FILES$_ENV$_COOKIE$_SESSION

$GLOBALS-Referencing all variables available in the global scope

$GLOBALS This global variable is used anywhere in the PHP script to access global variables (from functions or methods).

PHP stores all global variables in an array named $GLOBALS [index]. The name of the variable is the key of the array.

The following example shows how to use the Super global variable $GLOBALS:
Instance


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

PHP $_server

$_server This hyper-global variable holds information about the header, path, and location of the script.

The following example shows how to use some of the elements in $_server:
Instance


     
      echo$_SERVER['PHP_SELF'];echo"
";echo$_SERVER['SERVER_NAME'];echo"
";echo$_SERVER['HTTP_HOST'];echo"
";echo$_SERVER['HTTP_REFERER'];echo"
";echo$_SERVER['HTTP_USER_AGENT'];echo"
";echo$_SERVER['SCRIPT_NAME'];?>

The following table lists the most important elements that you can access in $_server:

PHP $_request

PHP $_request is used to collect data submitted by HTML forms.


 
  
  
   $_REQUEST 来收集 input 字段的值:
 
  

Instance

<html><body><formmethod="post"action="
    
     "><inputtype="text"name="fname"><inputtype="submit">
   
    form>
    
     $name = $_REQUEST['fname']; echo$name; ?>
    
     body>
   
    html>

PHP $_post

method="post" 的 HTML 表单后的表单数据。$_POST 也常用于传递变量。下面的例子展示了一个包含输入字段和提交按钮的表单。当用户点击提交按钮来提交数据后,表单数据会发送到 <form> 标签的 action 属性中指定的文件。在本例中,我们指定文件本身来处理表单数据。如果您希望使用另一个 PHP 页面来处理表单数据,请用更改为您选择的文件名。然后,我们可以使用超全局变量 $_POST 来收集输入字段的值:

Instance

<html><body><formmethod="post"action="
    
     "><inputtype="text"name="fname"><inputtype="submit">
   
    form>
    
     $name = $_POST['fname']; echo$name; ?>
    
     body>
   
    html>

PHP $_get

PHP $_GET 也可用于收集提交 HTML 表单 (method="get") 之后的表单数据。$_GET 也可以收集 URL 中的发送的数据。

Let's say we have a page with a hyperlink with parameters:

<html><body><ahref="test_get.php?subject=PHP&web=W3school.com.cn">测试 $GET
   
    a>
   
    body>
   
    html>
"Test $GET""subject""web""test_get.php"$_GET"test_get.php""test_get.php" 中的代码:实例
<html><body>
    
     echo"Study " . $_GET['subject'] . " at " . $_GET['web'];?>
    
     body>
   
    html>

The above describes the PHP global variables-hyper-global variables, including the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.