学习概要:
*了解基本的数组函数的使用
*懂得数组的遍历
*了解超全局数组的基本关系与使用
数组
1.数组定义和遍历<?php $arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9); echo "<pre>"; print_r ($arr); echo "</pre>"; ?>
print_r($arr)
$arr=array("name"=>1,3,"age"=>4,5,"100"=>6,7,"400"=>8,9); echo $arr['age']; echo "<br>"; echo $arr[100];
<?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]}</h1>"; } ?>
<?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]}</h1>"; }else{ echo "<h1 style='color:blue'>第".($i+1)."个人的名字是{$arr[$i]}</h1>"; } } ?>
<?php //键值对 name="user1" 就是数组下标和值,key和value $arr['name']="junzai"; $arr['age']=20; $arr['sex']="man"; $arr[]="abc"; echo "<pre>"; print_r ($arr); echo "</pre>"; foreach($arr as $key=>$val){ $num++; if($num%2==1){ echo "<h1>{$key}:{$val}</h1>"; }else{ echo "<h1 style='color:red'>{$key}:{$val}</h1>"; } } ?>
<?php header("content-type:text/html;charset=utf-8"); $arr=array("a","b",array("c","d"),array("e")); echo "<pre>"; print_r($arr); echo "</pre>"; echo "<hr>"; foreach($arr as $val){ if(is_array($val)){ foreach($val as $val2){ echo $val2."<br>"; } } else{ echo $val."<br>"; } } ?>
<?php header("content-type:text/html;charset=utf-8"); $arr=array("a","b",array("c","d"),array("e",array("f","z"))); echo "<pre>"; print_r($arr); echo "</pre>"; echo "<hr>"; foreach($arr as $val){ if(is_array($val)){ foreach($val as $val2){ if(is_array($val2)){ foreach($val2 as $val3){ echo $val3."<br>"; } }else { echo $val2."<br>"; } } } else{ echo $val."<br>"; } } ?>
<?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>"; print_r($row1); echo "</pre>"; ?>
<?php header("content-type:text/html;charset=utf-8"); echo "<pre>"; print_r($_SERVER); echo "</pre>"; ?>
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方式
2.a标签传值
只能用get方式
a标签推荐使用get方式提交数据
表单推荐使用post方式提交数据
magic_quotes_gpc = on;表示开启了get请求的时候,会讲get数据中的‘前面加\
get实例:
index.php
<html> <head> <title> 接收信息 </title> </head> <body> <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> </body> </html>
<html> <head> <title> 接收信息 </title> </head> <body> <h1>欢迎: <?php echo $_GET['name'];?> </h1> <hr> <h1>姓名:<?php echo $_GET['name']?></h1> <h1>年龄:<?php echo $_GET['age']?></h1> </body> </html>
<html> <head> <title> 接收信息 </title> </head> <body> <h1>提交用户信息</h1> <form action = "rev.php" method="POST"> 姓名:<input type="text" name="name" id=""><br> 年龄:<input type="text" name="age" id=""><br> <input type="submit" value="提交" target="_blank" > </form> </body> </html>
<html> <head> <header content-type="text/html";charset="gbk"> <title> 接收信息 </title> </head> <body> <h1>欢迎: <?php echo $_POST['name'];?> </h1> <hr> <h1>姓名:<?php echo $_POST['name']?></h1> <h1>年龄:<?php echo $_POST['age']?></h1> </body> </html>
<?php $username111="user1"; function show(){ $GLOBALS[username111]="USER2"; } show(); echo $username111; echo "<pre>"; print_r($GLOBALS); echo "</pre>"; ?>
转载请注明出处: http://blog.csdn.net/junzaivip
原文地址:http://blog.csdn.net/junzaivip/article/details/38770543