一,如何定义一个常量
关键字:define 语法 define('常量名','常量的值')
- <?
- $a=123;
- define('I',$a);
- echo I;
- ?>
二,数组
1定义一个数组
关键字array 语法 array(key=>value,key2=>value2,key3=>value3) key可以是×××或字符串,value可以是任意值
- <?
- $arr=array('too'=>'bar',123=>true);
- echo $arr['too'].'<br>';
- echo $arr[123];
- ?>
打印数组的方法 print_r 关键字:print_r 语法print_r(要打印数组名),主要用于调试
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('stu_no'=>'10010','stu_name'=>'老赵');
- print_r($stu);
- ?>
输出结果Array ( [stu_no] => 10010 [stu_name] => 老赵 )
输出方法 echo $数组名[key]
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('stu_no'=>'10010','stu_name'=>'老赵');
- echo $stu['stu_no'].'<br>';
- echo $stu['stu_name'];
- ?>
输出结果
10010
老赵
另外一种定义数组的方法 直接输入value值 key为从0起排列的×××
array('value','value2','value3','value4')
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('皮皮','乐乐','教皇','老赵');
- print_r($stu);
- ?>
输出结果为Array ( [0] => 皮皮 [1] => 乐乐 [2] => 教皇 [3] => 老赵 )
分别输出:
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('皮皮','乐乐','教皇','老赵');
- echo $stu[0].'<br>';
- echo $stu[1].'<br>';
- echo $stu[2].'<br>';
- echo $stu[3].'<br>';
- ?>
输出结果
皮皮
乐乐
教皇
老赵
循环输出方法 for语句 语法
for(循环条件) 例:$i=0;$i<4;$i++
{
echo $数组名[循环变量名]
}
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('皮皮','乐乐','教皇','老赵');
- for ($i=0;$i<4;$i++)
- {
- echo $stu[$i].'<br>';
- }
- ?>
输出结果
皮皮
乐乐
教皇
老赵
while循环 语法
$条件变量名=条件变量值
while(条件语句) 例子$i<4
{
$数组名[$条件变量名];
$条件变量++
}
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('皮皮','乐乐','教皇','老赵');
- $i=0;
- while ($i<4)
- {
- echo $stu[$i].' ';
- $i++;
- }
- ?>
输出结果 皮皮 乐乐 教皇 老赵
在数组末尾添加元素
语法 $数组名=array[value,value1,value2,value3];
$数组名[]=要添加的value;
$数组名[]=要添加的value;
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=array('皮皮','乐乐','教皇','老赵');
- $stu[]='浩民';
- $stu[]='苏超';
- $stu[]='吕腾';
- for ($i=0;$i<8;$i++)
- {
- echo $stu[$i].' ';
- }
- ?>
输出结果 皮皮 乐乐 教皇 老赵 浩民 苏超 吕腾
创建一个范围的数组range和count取得数组里有多少元素的方法
语法 $数组名=range(范围开始,范围结束)
count($数组名)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range(1,12);
- for ($i=0;$i<count($stu);$i++)
- {
- echo $stu[$i].' ';
- }
- ?>
输出结果 1 2 3 4 5 6 7 8 9 10 11 12
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range('a','z');
- for ($i=0;$i<count($stu);$i++)
- {
- echo $stu[$i].' ';
- }
- ?>
输出结果 a b c d e f g h i j k l m n o p q r s t u v w x y z
三 填充数组
array_pad
语法
array_pad($数组名,数组长度,填充默认值)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range('0','3');
- $stu2=array_pad($stu,7,0);
- for ($i=0;$i<count($stu2);$i++)
- {
- echo $stu2[$i].' ';
- }
- ?>
输出结果 0 1 2 3 0 0 0
五,在数组中删除和插入替换元素 array_splice
array_splice接两个参数代表删除 接四个参数代表插入或替换(第三个参数为0的时候为插入,不为0时为替换)
删除语法 array_splice($数组名,删除结束下标) 下标等于在这个Key之前的删除
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range('0','12');
- $stu2=array_splice($stu,5);
- for ($i=0;$i<count($stu2);$i++)
- {
- echo $stu2[$i].' ';
- }
- ?>
输出结果 5 6 7 8 9 10 11 12
插入语法 array_splice($被插入数组名,下标,0,$插入的新数组名) 下标为在这个下标到上一个之前插入新数组
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range('0','12');
- $stu2=array('a','b','c');
- array_splice($stu,5,0,$stu2);
- for ($i=0;$i<count($stu);$i++)
- {
- echo $stu[$i].' ';
- }
- ?>
输出结果 0 1 2 3 4 a b c 5 6 7 8 9 10 11 12
替换语法 array_splice($被替换数组名,key,key2,$替换的新数组名) 下标1下标2结合在一起表示从下标key开始数下标key2个元素被新的数组替换
- <?
- header("Content-Type:text/html; charset=utf-8");
- $stu=range('0','12');
- $stu2=array('a','b','c');
- array_splice($stu,3,6,$stu2);
- for ($i=0;$i<count($stu);$i++)
- {
- echo $stu[$i].' ';
- }
- ?>
输出结果为 0 1 2 a b c 9 10 11 12
从3开始数6个元素被替换成 a,b,c

















