Home> php教程> php手册> body text

PHP基本语法第二章

WBOY
Release: 2016-06-13 10:43:27
Original
852 people have browsed it

一,如何定义一个常量

关键字: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'].'
'; 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'].'
'; 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].'
'; echo $stu[1].'
'; echo $stu[2].'
'; echo $stu[3].'
'; ?> 输出结果

皮皮

乐乐

教皇

老赵

循环输出方法for语句 语法

for(循环条件) 例:$i=0;$i

{

echo $数组名[循环变量名]

}

header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); for ($i=0;$i'; } ?> 输出结果

皮皮

乐乐

教皇

老赵

while循环 语法

$条件变量名=条件变量值

while(条件语句) 例子$i

{

$数组名[$条件变量名];

$条件变量++

}

header("Content-Type:text/html; charset=utf-8"); $stu=array('皮皮','乐乐','教皇','老赵'); $i=0; while ($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 输出结果 皮皮 乐乐 教皇 老赵 浩民 苏超 吕腾

创建一个范围的数组range和count取得数组里有多少元素的方法

语法$数组名=range(范围开始,范围结束)

count($数组名)

header("Content-Type:text/html; charset=utf-8"); $stu=range(1,12); for ($i=0;$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 输出结果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 输出结果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 输出结果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 输出结果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 输出结果为0 1 2 a b c 9 10 11 12

从3开始数6个元素被替换成a,b,c

本文出自 “PHP学习笔记” 博客

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!