导读:
PHP也有逻辑判断结构:if 、 if……else、if...elseif...、switch
PHP中也有逻辑判断结构:while、do……while、for、foreach
一、foreach
foreach($array as $value){}
foreach 只适用于数组,用于遍历数组中的每一个键/值。
二、函数
自定义函数关键点是:函数名、参数、返回值。
函数名必须以字母或下划线开头,php对函数名的大小写不敏感;参数和变量类似;参数可以设置默认值;
三、数组
3.1 PHP中可以定义三种类型的数组。
索引数组——带有数字索引的数组,如:$array01 = array("aaa","bb","ccc");
关联数组——带有指定键的数组,如:$array02 = array("a"->"aa","b"->"bbb","c"->"ccc");
多维数组——包含一个或多个数组的数组。
3.2 创建数组通过array()函数,获取数组的长度,通过count($array)方法。
3.3 遍历关联数组:
for ($array02 as $key->$value){}
3.4 对数组进行排序
sort()升序排序、rsort()降序排序、asort()对数组值升序排序、arsort()对数组值降序排序、ksort()对数组键升序排序、krsort()对数组键降序排序
四、超全局变量
PHP中很多预定义变量都是超全局变量。无须任何代码,就可以在任何函数、类或文件访问它们。
4.1 $GLOBALS
4.2 $_SERVER
4.3 $_REQUEST
4.4 $_POST
4.5 $_GET
五、时间
date(form,timestrip)、date_default_timezone_set()、mktime、strtotime()
5.1 date() ,根据时间戳返回当前时间,第二个参数可以不写,默认是当前时间戳
# Y m d h i s a
$nowDatetime = date("Y-m-d h:i:s a");
echo $nowDatetime;
5.2 date_default_timezone_set(),用于设置时区
如果使用date()返回的时间不正确,可能是因为服务器位于其他国家或被设置成其他时区
/*
如果使用date()返回的时间不正确,可能是因为服务器位于其他国家或被设置成其他时区,
设置正确的时区之后,就可以返回正确的值了。
*/
date_default_timezone_set("Asia/Shanghai");
# date
# Y m d h i s a
$nowDatetime = date("Y-m-d h:i:s a");
echo $nowDatetime;
echo "<br>";
echo "now is ".date("H:i:sa")."<br>";
5.3 mktime() 返回之间戳
mktime(hour,minute,second,month,day,year);
# mktime 返回时间戳
# mktime(hour,minute,second,month,day,year);
$d = mktime(5,30,00,12,18,2018);
echo $d;
echo "<br>";
echo date("Y-m-d",$d);
5.4 strtotime() 使用字符串创建时间
# 使用字符串创建时间
$d02 = strtotime("2018-10-11");
echo "<br>".date("Y-m-d H:i:s",$d02)."<br>";
六、php中的包含
通过include或require语句,可以将php的文件内容插入到另一个文件中。
include:包含文件如果不存在,会继续加载页面;
require:包含文件如果不存在,会停止加载页面。
<body>
<h1>Welcome to here.</h1>
<?php
//include "footera.php";
echo "<br>require<br>";
require "footera.php";
echo "aaa:<br>$color,$car<br>";
echo "I have a dream.";
?>
</body>
当需要包含的文件不是必须的时候,使用include,否则使用require。
七、读取文件
7.1 打开,关闭文件
fopen(),fclose()
# 打开文件
$myFile = fopen("readyFile.txt","r") or die("file not exists.");
# 读取文件内容
// fread() 第一个参数,是打开的文件,第二个参数要读去的文件字节数
echo filesize("readyFile.txt")."<br>";
echo fread($myFile,filesize("readyFile.txt"));
# 关闭文件
fclose($myFile);
7.2 读取一行
fgets()
#打开文件
$myFile = fopen("readyFile.txt","r+") or die("file not exists");
// 读取单行文件
echo "<br>read one line.<br>";
echo fgets($myFile);
# 关闭文件
fclose($myFile);
7.3 一行一行,循环读取
feof() 、fgets()
echo "<p>循环读取文件</p>";
#打开文件
$myFile = fopen("readyFile.txt","r+") or die("file not exists");
// feof() 检查指针是否到达文件最后
while(!feof($myFile)){
echo fgets($myFile);
echo "<br>";
}
echo fgets($myFile);
# 关闭文件
fclose($myFile);
7.4 一个字符一个字符,循环读取文件
feof()、fgetc()
echo "<p>读取单个字符,知道文件末尾</p>";
#打开文件
$myFile = fopen("readyFile.txt","r+") or die("file not exists");
// 读取单个字符,直到文件末尾
while(!feof($myFile)){
echo fgetc($myFile);
}
fclose($myFile);
7.5 覆盖式的写入文件,如果文件不存在,就创建一个新文件
echo "<p>向文件中写入内容</p>";
$myFile = fopen("writeFile.txt","w") or die("Write to file failed.");
fwrite($myFile,"Li Fei\r");
fwrite($myFile,"zy");
fclose($myFile);
7.6 向文件里追加内容,如果文件不存在,就创建一个新文件
echo "<p>向位你就爱你中追加内容</p>";
$myFile = fopen("writeFile.txt","a") or die("write file failed");
fwrite($myFile,"\rI have a dream.");
fclose($myFile);