PHP基本语法第二章
一,如何定义一个常量
关键字: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
header("Content-Type:text/html; charset=utf-8"); $stu=range('a','z'); for ($i=0;$i
三 填充数组
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
五,在数组中删除和插入替换元素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
插入语法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
替换语法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
从3开始数6个元素被替换成a,b,c
本文出自 “PHP学习笔记” 博客

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment