Rumah > php教程 > php手册 > teks badan

第六章_PHP数组(二)

WBOY
Lepaskan: 2016-06-13 10:18:00
asal
980 orang telah melayarinya

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

  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> ?>
Salin selepas log masuk

  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> ?>
Salin selepas log masuk

  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> ?>
Salin selepas log masuk

  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> ?>
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan