PHP第六课 数组的用法
学习概要: *了解基本的数组函数的使用 *懂得数组的遍历 *了解超全局数组的基本关系与使用 数组 1.数组定义和遍历 2.数组函数 数组定义: $arr=array(1,2,3);//索引数组,下标全是数字 $arr=array(name=user1,age=30);//关联数组,下标中包含字母 //下标只有两种
学习概要:
*了解基本的数组函数的使用
*懂得数组的遍历
*了解超全局数组的基本关系与使用
数组
1.数组定义和遍历2.数组函数
数组定义:
$arr=array(1,2,3);//索引数组,下标全是数字
$arr=array("name"=>"user1","age"=>"30");//关联数组,下标中包含字母
//下标只有两种,要么是字母,要是是不带双引号的数字
<?php $arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9); echo "<pre class="brush:php;toolbar:false">"; print_r ($arr); echo "
数组下标:
如果是字母
$arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9);
//下标打印:"name" 0
[name] => 1
[0] => 3
[age] => 4
[1] => 5
[100] => 6
[101] => 7
[400] => 8
[401] => 9
数组取值:
1.输出整个数组
print_r($arr)
2.输出数组中的某个值
$arr=array("name"=>1,3,"age"=>4,5,"100"=>6,7,"400"=>8,9); echo $arr['age']; echo "<br>"; echo $arr[100];
3.数组赋值:
1.$arr['age']=30;
数组赋值也可以定义数组:
$arr[]=1;
$arr[]=2;
4.数组遍历:
1.for循环
<?php $arr[]=1; $arr[]=2; $arr[]=3; $arr[]=4; $arr[]=5; $arr[]=6; for($i=0;$i<5;$i++){ echo "<h1>第".($i+1)."个人的名字是{$arr[$i]}"; } ?>
循环加判断:
<?php $arr[]=1; $arr[]=2; $arr[]=3; $arr[]=4; $arr[]=5; $arr[]=6; for($i=0;$i<5;$i++){ if($i%2==0){ echo "<h1>第".($i+1)."个人的名字是{$arr[$i]}"; }else{ echo "<h1 id="第-i-个人的名字是-arr-i">第".($i+1)."个人的名字是{$arr[$i]}</h1>"; } } ?>
2.foreach循环
foreach 进行数组遍历:
<?php //键值对 name="user1" 就是数组下标和值,key和value $arr['name']="junzai"; $arr['age']=20; $arr['sex']="man"; $arr[]="abc"; echo "<pre class="brush:php;toolbar:false">"; print_r ($arr); echo "
{$key}:{$val}
"; }else{ echo "{$key}:{$val}
"; } } ?>3.while....list ..each 循环遍历
while(list($key,$val)=each($arr)){
echo $key.$val;
}
//推荐使用foreach遍历数组
多维数组:
1.一维数组 $arr=array(1,2,3);
$arr[0];
2.二维数组 $arr=array(1,2,array(4,5));
$arr[2][0];
2.二维数组 $arr=array(1,2,array(3,array(4,5)));
$arr[2][1][0];
二维数组遍历:
<?php header("content-type:text/html;charset=utf-8"); $arr=array("a","b",array("c","d"),array("e")); echo "<pre class="brush:php;toolbar:false">"; print_r($arr); echo "
"; foreach($arr as $val){ if(is_array($val)){ foreach($val as $val2){ echo $val2."
"; } } else{ echo $val."
"; } } ?>
三维数组取值:
<?php header("content-type:text/html;charset=utf-8"); $arr=array("a","b",array("c","d"),array("e",array("f","z"))); echo "<pre class="brush:php;toolbar:false">"; print_r($arr); echo "
"; foreach($arr as $val){ if(is_array($val)){ foreach($val as $val2){ if(is_array($val2)){ foreach($val2 as $val3){ echo $val3."
"; } }else { echo $val2."
"; } } } else{ echo $val."
"; } } ?>
//推荐使用一维数组和二维数组
一张数据表其实就是一个二维数组,里面的每一行记录就是一个一维数组
查询数据库:
<?php header("content-type:text/html;charset=utf-8"); mysql_connect("localhost","root","1234"); mysql_select_db("test"); mysql_query("set names utf8"); $sql = "select * from user"; $result = mysql_query($sql); $row1 = mysql_fetch_assoc($result); echo "<pre class="brush:php;toolbar:false">"; print_r($row1); echo "
超全局数组:
超全局数组
$_SERVER
$_GET
$_POST
$_REQUEST
$_FILES
$_COOKIES
$_SESSION
$GLOBALS
$_SERVER 查看服务器信息
<?php header("content-type:text/html;charset=utf-8"); echo "<pre class="brush:php;toolbar:false">"; print_r($_SERVER); echo "
Apache/2.2.8 (Win32) PHP/5.2.6 Server at localhost Port 80
[SERVER_SOFTWARE] => Apache/2.2.8 (Win32) PHP/5.2.6
[SERVER_NAME] => localhost//服务器域名
[SERVER_ADDR] => 127.0.0.1//服务器ip
[SERVER_PORT] => 80//端口号
[REMOTE_ADDR] => 127.0.0.1 //客户端访问ip
[DOCUMENT_ROOT] => E:/AppServ/www
[SERVER_ADMIN] => goxuexi@126.com
[SCRIPT_FILENAME] => E:/AppServ/www/index.php //脚本文件名字的绝对路径
[REMOTE_PORT] => 49881
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] => //请求字符串
[REQUEST_URI] => ///请求url地址
[SCRIPT_NAME] => /index.php//脚本名称(相对网站根目录)
[PHP_SELF] => /index.php
[REQUEST_TIME] => 1407568551//访问时间
[argv] => Array
(
)
[argc] => 0
)
$_GET 获取用get提交过来的数据
http://localhost/index.php?id=10&name=user1
两个页面之间通讯:
1.表单传值
第一种:get方式
第二种:post方式
只能用get方式
表单推荐使用post方式提交数据
magic_quotes_gpc = on;表示开启了get请求的时候,会讲get数据中的'前面加\
get实例:
index.php
<title> 接收信息 </title> <a href="rev.php?name=junjun2&age=26" target="'_blank">junjun2</a><br> <a href="rev.php?name=junjun3&age=23" target="'_blank">junzai3</a><br> <a href="rev.php?name=junjun4&age=25" target="'_blank">junjun4</a><br> <a href="rev.php?name=junjun5&age=27" target="'_blank">junjun5</a><br>
rev.php
<title> 接收信息 </title> <h1>欢迎: <?php echo $_GET['name'];?> </h1> <hr> <h1>姓名:<?php echo $_GET['name']?> </h1> <h1>年龄:<?php echo $_GET['age']?> </h1>
post实例
$_POST:获取表单post过来的数据
index.php
<title> 接收信息 </title> <h1 id="提交用户信息">提交用户信息</h1>
rev.php
<header content-type="text/html"> <title> 接收信息 </title> <h1>欢迎: <?php echo $_POST['name'];?> </h1> <hr> <h1>姓名:<?php echo $_POST['name']?> </h1> <h1>年龄:<?php echo $_POST['age']?> </h1> </header>
$_REQUEST
获取a或者表单get或post过来的数据.
$_COOKIES
同一个页面在多个页面获取
$_SESSION
同一个变量在多个页面获取到
$_FILES
获取表单中的文件,并生成一个数组.
$GLOBALS
$GLOBALS[_SERVER]
$GLOBALS[_GET]
$GLOBALS[_POST]
$GLOBALS[_FILES]
$GLOBALS[_REQUEST]
$GLOBALS[_COOKIES]
$GLOBALS[username]//里面包含页面内的全局变量,并且通过$GLOBALS[username]="user2"改变$username的值.
实例:使用$GLOBALS改变全局变量的值.
<?php $username111="user1"; function show(){ $GLOBALS[username111]="USER2"; } show(); echo $username111; echo "<pre class="brush:php;toolbar:false">"; print_r($GLOBALS); echo "

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

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

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