Home > php教程 > php手册 > body text

第六章_PHP数组(二)

WBOY
Release: 2016-06-13 10:18:00
Original
980 people have browsed it

  这篇随笔是对预定义数组变量的总结。通过预定义数组变量,我们可以获得系统环境、用户对话、表单数据等信息。

  1.服务器变量:$_SERVER

  利用foreach语句打印$_SERVER中的所有元素:

<span 1</span> <?<span php
</span><span 2</span>     <span foreach</span>(<span $_SERVER</span> <span as</span> <span $key</span> => <span $value</span><span ){
</span><span 3</span>         <span echo</span> <span $key</span>.' => '.<span $value</span>.'<br>'<span ;
</span><span 4</span> <span     }
</span><span 5</span> ?>
Copy after login

  2.环境变量: $_ENV(无输出结果)

  3.GET变量:$_GET

<span  1</span> <form action="<?<span $_SERVER</span>['PHP_SELF']?>" method="GET">
<span  2</span>     姓名:<input type="text" name="name" size="15"/>
<span  3</span>     年龄:<input type="text" name="age" size="15"/>
<span  4</span>     爱好:<input type="text" name="hobby" size="15"/>
<span  5</span>     <input type="submit" name="submit" value="提交"/>
<span  6</span> </form>
<span  7</span> <?<span php
</span><span  8</span>     <span if</span>(<span isset</span>(<span $_GET</span>['submit'<span ])){
</span><span  9</span>         <span echo</span> '<p>'<span ;
</span><span 10</span>         <span echo</span> '姓名:'.<span $_GET</span>['name'].'<br>'<span ;
</span><span 11</span>         <span echo</span> '年龄:'.<span $_GET</span>['age'].'<br>'<span ;
</span><span 12</span>         <span echo</span> '爱好:'.<span $_GET</span>['hobby'].'<br>'<span ;
</span><span 13</span> <span     }
</span><span 14</span> ?>
Copy after login

  4.POST变量:$_POST

  通过POST方式传递数据时,数据不会显示在URL中,因此相比GET方式更安全。而且,POST方式传递的数据量理论上没有限制。

<span  1</span> <form action="<?<span $_SERVER</span>['PHP_SELF']?>" method="POST">
<span  2</span>     姓名:<input type="text" name="name" size="15"/>
<span  3</span>     年龄:<input type="text" name="age" size="15"/>
<span  4</span>     爱好:<input type="text" name="hobby" size="15"/>
<span  5</span>     <input type="submit" name="submit" value="提交"/>
<span  6</span> </form>
<span  7</span> <?<span php
</span><span  8</span>     <span if</span>(<span isset</span>(<span $_POST</span>['submit'<span ])){
</span><span  9</span>         <span echo</span> '<p>'<span ;
</span><span 10</span>         <span echo</span> '姓名:'.<span $_POST</span>['name'].'<br>'<span ;
</span><span 11</span>         <span echo</span> '年龄:'.<span $_POST</span>['age'].'<br>'<span ;
</span><span 12</span>         <span echo</span> '爱好:'.<span $_POST</span>['hobby'].'<br>'<span ;
</span><span 13</span> <span     }
</span><span 14</span> ?>
Copy after login

  5.文件上传变量:$_FILES(代码提示有误,无法找到上传文件)

<span  1</span> <form enctype="multipart/form-data"  action="<?=<span $_SERVER</span>['PHP_SELF']?>" method="POST">
<span  2</span>       <input type="hidden" name="MAX_FILE_SIZE" value="104857600" /><!--100M -->
<span  3</span>       上传文件:<input name="upload_file" type="file" size="50"/>
<span  4</span>       <input type="submit" name="submit" value="上传" />    
<span  5</span> </form>
<span  6</span> <?<span php
</span><span  7</span>     <span if</span>(<span isset</span>(<span $_POST</span>['submit'<span ])){
</span><span  8</span>         <span echo</span> <span $_FILES</span>['uplaod_file']['error']==UPLOAD_ERR_OK ? '上传成功!<br>': '上传失败!<br>'<span ;
</span><span  9</span>         <span echo</span> '上传文件名:'.<span $_FILES</span>['upload_file']['name'].'<br>'<span ;
</span><span 10</span>         <span echo</span> '上传文件大小:'.<span $_FILES</span>['upload_file']['size'].'字节<br>'<span ;
</span><span 11</span>         <span echo</span> '临时文件名:'.<span $_FILES</span>['upload_file']['tmp_name'].'<br>'<span ;
</span><span 12</span> <span     }
</span><span 13</span> ?>
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template