1.3.2 PHP中的预定义变量

什么是预定义变量

PHP提供给大家直接就可以使用的变量,所有的预定义变量都是全局变量

预定义变量的分类

  • $GLOBALS 超全局变量,包含以下所有的预定义变量
  • $_SERVER 服务器和执行环境信息变量
  • $_ENV 环境变量
  • $_COOKIE HTTP Cookies
  • $_SESSION HTTP Session变量
  • $_FILES 文件上传信息变量
  • $_GET HTTP GET变量;主要接收以?形式传递的数据,像表单以get形式发送数据,包括像超链接典型的?形式传递参数;$_GET['名称']
  • $_POST HTTP POST变量;主要接收表单以post形式发送的数据;$_POST['名称']
  • $_REQUEST $_GET+$_POST+$_COOKIE

    get练习

    新建一个html文件

    命名为“reg-get.html”,复制下面的代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>注册页面</title>
    </head>
    <body>
    <h1>注册页面</h1>
    <form action="doReg-get.php" method="get">
      <table border="1" width="70%" cellpadding="0" cellspacing="0" bgcolor="#abcdef">
        <tr>
          <td align="right">用户名</td>
          <td><input type="text" name="username" id="" placeholder="请输入合法用户名..."></td>
        </tr>
        <tr>
          <td align="right">密码</td>
          <td><input type="password" name="password" id="" placeholder="请输入密码..."></td>
        </tr>
        <tr>
          <td align="right">邮箱</td>
          <td><input type="email" name="email" id="" placeholder="请输入合法邮箱..."></td>
        </tr>
        <tr>
          <td align="right">性别</td>
          <td>
            <input type="radio" name="sex" id="" value='男'>男
            <input type="radio" name="sex" id="" value='女'>女
            <input type="radio" name="sex" id="" value='保密'>保密
          </td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="立即注册"></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

    新建一个php文件

    命名为“doReg-get.php”,复制下面代码

    <?php
    header('content-type:text/html;charset=utf-8');
    //接收表单发送过来的数据
    //$_GET接收表单以get形式发送过来的数据,$_GET['名称']
    echo 'this is a test';
    echo '用户名:',$_GET['username'],'<br/>';
    echo '密码:',$_GET['password'],'<br/>';
    echo '邮箱:',$_GET['email'],'<br/>';
    echo '性别:',$_GET['sex'],'<br/>';

    填写注册表单进行测试

    在浏览器中打开“reg-get.html”,填写表单后点击“立即注册”按钮,即可跳转到对应的php文件,并显示你刚才填写的表单内容。

【在线测试】

post练习

新建一个html文件

命名为“reg-post.html”,复制下面的代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>注册页面</title>
  </head>
  <body>
    <h1>注册页面</h1>
    <form action="doReg-post.php" method="post">
      <table border="1" width="70%" cellpadding="0" cellspacing="0" bgcolor="#abcdef">
        <tr>
          <td align="right">用户名</td>
          <td><input type="text" name="username" id="" placeholder="请输入合法用户名..."></td>
        </tr>
        <tr>
          <td align="right">密码</td>
          <td><input type="password" name="password" id="" placeholder="请输入密码..."></td>
        </tr>
        <tr>
          <td align="right">邮箱</td>
          <td><input type="email" name="email" id="" placeholder="请输入合法邮箱..."></td>
        </tr>
        <tr>
          <td align="right">性别</td>
          <td>
            <input type="radio" name="sex" id="" value='男'>男
            <input type="radio" name="sex" id="" value='女'>女
            <input type="radio" name="sex" id="" value='保密'>保密
          </td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="立即注册"></td>
        </tr>
      </table>
    </form>
  </body>
</html>

新建一个php文件

命名为“doReg-post.php”,复制下面代码

<?php
header('content-type:text/html;charset=utf-8');
//接收表单发送过来的数据
//$_POST接收表单以post形式发送过来的数据,$_POST['名称']
 echo 'this is a test';
 echo '用户名:',$_POST['username'],'<br/>';
 echo '密码:',$_POST['password'],'<br/>';
 echo '邮箱:',$_POST['email'],'<br/>';
 echo '性别:',$_POST['sex'],'<br/>';

填写注册表单进行测试

在浏览器中打开“reg-post.html”,填写表单后点击“立即注册”按钮,即可跳转到对应的php文件,并显示你刚才填写的表单内容。

【在线测试】

REQUEST说明

$_REQUEST 既可以接收get请求,又可以接收post请求,还能接收cookie数据

其他练习

搜索表单

新建html文件命名为“googleSearch.html”,复制代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Google Search</title>
  </head>
  <body>
    <h1>Google Search</h1>
    <form  action="doSearch.php" method="get">
      <input type="search" name="keyword" placeholder="请输入您要搜索的内容"/>
      <input type="submit" value="搜索"/>
    </form>
  </body>
</html>

新建php文件命名为“doSearch.php”,复制代码

<?php
header('content-type:text/html;charset=utf-8');
//接收以?形式传递的数据,需要通过$_GET['名称']
$keyword=$_GET['keyword'];
echo '用户搜索的关键字为:',$keyword,'<br/>';

【在线测试】

本文是全系列中第8 / 24篇:PHP快速入门

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

滚动至顶部