第六章_PHP数组(二)
这篇随笔是对预定义数组变量的总结。通过预定义数组变量,我们可以获得系统环境、用户对话、表单数据等信息。
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> ?>
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> ?>
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> ?>
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> ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
