-
-
- 字符串有关常用函数
-
·输出与格式化:echo , print, printf, print_r, var_dump.
·字符串去除与填充:trim, ltrim, rtrim, str_pad
·字符串连接与分割:implode, join, explode, str_split
·字符串截取:substr, strchr, strrchr,
·字符串替换:str_replace, substr_replace
·字符串长度与位置: strlen, strpos, strrpos,
·字符转换:strtolower, strtoupper, lcfirst, ucfirst, ucwords
·特殊字符处理:nl2br, addslashes, htmlspecialchars, htmlspecialchars_decode,
手册》函数参考》文本处理》字符串》字符串函数
-
-
- 常用数学函数(重点)
-
max: 取得若干个数据中的最大值
min: 取得若干个数据中的最小值
round: 对某个数据进行四舍五入(可以设定保留几位小数)
ceil: 对某个数“向上取整”:将一个数据往上找出其小的一个整数(含其本身)。
floor: 对某个数“向下取整”:将一个数据往下找出其大的一个整数(含其本身)
$n1 = floor(4.1); //4
$n2 = floor(4.9); //4
$n3 = floor(4); //4
$n4 = floor(-4.1); //-5
abs: 取得某个数据的绝对值
sqrt: 计算某个数的开方值
pow: 对某个数进行“幂运算”(就是获得某个数的若干次方)
$n1 = pow(3, 2); //3的2次方,9
$n2 = pow(2, 3); //8
$n3 = pow(1.5, 2); //2.25
$n4 = pow(1.5, 2.5); //。。。。。1.5的2.5次方
$n5 = pow(9, 0.5); //3,就是开方,相当于sqrt()
rand: 获得某两个数之间的随机整数(含该两个数)
mt_rand: 获得某两个数之间的随机整数(含该两个数),比rand更快。
$n1 = mt_rand(0, 10); //随机数在0-10之间(含)
手册》函数参考》数学扩展》Math》Math函数。
演示案例:
定义一个函数,该函数可以返回所给定的任意两个数字之间的随机整数。
-
-
- 常用时间函数
-
·time:获得当前时间(精确到秒),结果其实一个“整数”而已,代表从1970年1月1日0:0:0秒到当前时刻的秒数。
·microtime:获得当前时间(可以精确到微秒)
有两个用法:
microtime(true):获得秒数(跟time一样),是一个数字(浮点数,有4位小数)
microtime(false):获得也是秒数,但因为精度太高,导致浮点数无法表达出来,以致返回的是一个字符串。
·mktime:创建一个时间数据,参数为:时、分、秒,月、日、年
$t1 = mktime(10, 8, 5, 7, 12, 2018);
·date:将一个时间转换为某种字符串形式
date(“Y-m-d H:i:s”);
·idate:取得一个时间的某个单项数据值,比如idate(“Y”)取得年份数
·strtotime:将一个字符串“转换”为时间值;
·date_default_timezone_set:在代码中设置“时区”
·date_default_timezone_get:在代码中获取“时区”
案例:
课堂电脑性能大比拼:
计算从1加到1000万,看花了多少时间?
做法:先获得一个时间,然后计算,然后再获得一个时间,后一个时间,减前一个时间,就是耗时。
$t1 = microtime(true); //获得当前时间
$n = 0;
for($s=1;$s<=10000000;$s++)
{
$n+=$s;
}
$t2 = microtime(true); //获得当前时间
$t3 = $t2 - $t1;
echo "$n <br>";
echo $t3;