PHP自定义函数+系统函数库
全局变量
$n = 5; //全局变量 function fun1(){ global $n; echo ‘我在函数体内也可以调用全局变量n,它的值是:‘ , $n;//5 $n++; } fun1(); echo ‘<hr>‘; echo $n;//6
$n = 6; function fun1(){ echo ‘变量的值是:‘ , $GLOBALS[‘n‘]; $GLOBALS[‘n‘]++; } fun1(); echo $GLOBALS[‘n‘];
不使用循环语句,来计算1~100的和
function recursive($n){ if($n>=1){ return $n + recursive($n-1); } } echo recursive(100);
引用
$foo = ‘Bob‘; $bar = &$foo; //看待成变量的别名 $bar = ‘Rose‘; echo $foo;//Rose $foo = ‘Mooc‘; $bar = &$foo; //看待成变量的别名 unset($foo); //变量销毁 echo $bar;//Mooc
自定义函数
function fun1(&$n){ $n++; echo ‘我是函数体内的局部变量‘ , $n ;//4 } $n = 3; fun1($n); echo $n , ‘<hr>‘;//4
获得扩展名
function getExtension($filename) { $pos = strrpos($filename, ‘.‘); $extension = strtolower(substr($filename, $pos + 1)); return $extension; } $path = ‘mooc.func.pHP‘; var_dump(getExtension($path));
求平均数
function avg(...$args) { return $args; } var_dump(avg(1, 2, 3));
系统函数库
字符串转数组
$str = ‘A|B|C|D‘; $arr = explode(‘|‘, $str); print_r($arr);//[A,B,C,D]
数组转字符串
$arr2 = array(‘Tom‘,‘John‘,‘Rose‘); $str2 = implode(‘,‘,$arr2); echo $str2;//Tom,John,Rose
获取扩展名:
方法一
$filename = ‘ab.cd.gif.JpEg‘; //gepj.fig.dc.ba $num = strrpos($filename, ‘.‘); echo strtolower(substr($filename, $num+1)) , ‘<br/><br/>‘;//jpeg
方法二
$filename = ‘ab.cd.gif.JpEg‘; //gepj.fig.dc.ba $str2 = strrev($filename);//strrev反转字符串 $num = strpos($str2, ‘.‘); echo strtolower(strrev(substr($str2, 0,$num)));//jpeg
trim移除字符串两侧的字符
$str = "\n\n\t\tABC\t\t"; echo trim($str);//ABC
md5()加密
$str = ‘abc‘; echo md5($str);//900150983cd24fb0d6963f7d28e17f72
格式化字符串
$number = 5; $str = ‘shanghai‘; $txt = sprintf(‘there are %d million cars in %s‘,$number,$str); echo $txt;//there are 5 million cars in shanghai $number = 123; $txt = sprintf("带有两位小数的结果是:%1\$.2f,\n不带小数的是:%1\$d",$number); echo $txt;//带有两位小数的结果是:123.00,不带小数的是:123
htmlspecialchars特殊字符转为HTML实体
$str = "A>B,B<C,Tom&John,He said:\"I‘m OK\""; echo htmlspecialchars($str,ENT_QUOTES);//A>B,B<C,Tom&John,He said:"I'm OK"
通过str_replace进行转换
$str1 = str_replace(‘&‘, ‘&‘, $str); //必须是第一阶梯 $str2 = str_replace(‘>‘, ‘>‘, $str1); $str2 = str_replace(‘<‘, ‘<‘, $str2); $str2 = str_replace(‘"‘, ‘"‘, $str2); $str2 = str_replace(‘\‘‘, ‘'‘, $str2); echo $str2;//A>B,B<C,Tom&John,He said:"I'm OK"
str_ireplace不区分大小写
$str = ‘javascript‘; echo str_ireplace(‘A‘, ‘b‘, $str);//jbvbscript
随机地打乱字符串中的所有字符
$str = ‘abcdefghijklmnopqrstuvwxyz‘; $str = str_shuffle($str); echo substr($str,0,4);//drif
strlen获得字符长度
$str1 = NULL;//0 $str2 = ‘AB‘;//2 $str3 = ‘中国‘;//6 一个中文3个字符 echo strlen($str1) , strlen($str2) , strlen($str3);
stripos不区分大小写,字符串从0开始编号,如果没有出现,则返回FALSE
$str1 = ‘javascript‘; $str2 = ‘A‘; var_dump(stripos($str1, $str2)); //int(1)
搜索$str2在字符串中的位置,并返回从该位置到字符串结尾的所有字符
$str1 = ‘abcdcef‘; $str2 = ‘c‘; echo strrchr($str1, $str2);//cef
获取扩展名
$filename = ‘a.bc.cd.png‘; echo substr(strrchr($filename, ‘.‘),1);//png
strtoupper转大写
strtolower转小写
$str1 = ‘html‘; $str2 = ‘PHP‘; echo strtoupper($str1) , strtolower($str2);//HTMLphp
ucfirst句子首字母大写
ucwords单词首字母大写
$str3 = ‘this is a test‘; echo ucfirst($str3) , ucwords($str3);
substr截取字符串
负数=字符串长度+该负数
$str = ‘javascript‘; echo strlen($str);//10 echo substr($str, 0,4) ;//java echo substr($str, 4);//script echo substr($str, -2);//pt -2=10-2=8 echo substr($str, -5,-2) , "\n";//cri -5,-2=5,8
将字符串转为Zend_Controller_Front
$str = ‘ZenD_CONTRollER_FronT‘; //1.转换小写 $str1 = strtolower($str); //2.将下划线替换成空格 $str2 = str_replace(‘_‘, ‘ ‘, $str1); //3.通过ucwords进行首字母大写操作 $str3 = ucwords($str2); //4.将空格替换成下划线 $str4 = str_replace(‘ ‘, ‘_‘, $str3); echo $str4;//Zend_Controller_Front
floor() ceil()
$x = 2.7; $y = 3.01; echo floor($x) , ‘<br/><br/>‘;//2 向下取整 echo ceil($y) , ‘<br/><br/>‘;//4 向上取整
假设记录数为X,每页显示Y条记录,求总页数z
z = ceil(X/Y);
fmod()对浮点数取模
echo fmod(7.8,3) , ‘<br/>‘;//1.8
对整数取模
echo 7.8 % 3 ; //整数余数的操作//1
格式化数字
$x = 7896.827; echo number_format($x) , ‘<br/><br/>‘;//7,897 echo number_format($x,2) , ‘<br/><br/>‘;//7,896.83
pow()幂操作 sqrt()平方根操作
echo pow(2,3);//8 echo sqrt(4) ;//2
mt_rand()是更好的随机数生成器,因为它跟rand()相比播下了一个更好地随机数种子;而且性能上比rand()快4倍
echo rand(50,80); echo mt_rand(10,99);
生成四位数随机验证码
$chars = ‘abcdefghijlmnopqrstuvwxyz789654321‘; $len = strlen($chars); for($i=0;$i<4;$i++){ $char .= substr($chars,mt_rand(0,$len-1),1); } echo $char;
round()四舍五入
$x = 7.238; echo round($x);//7 echo round($x,2);//7.24
strtotime字符串转时间
echo ‘当前日期:‘ , date(‘Y-m-d‘) , "\n";//2020-01-10 echo ‘下个月的日期:‘ , date(‘Y-m-d‘, strtotime(‘1 month‘)) , "\n";//2020-02-10 echo ‘上个月最后一天:‘ , date(‘Y-m-d H:i:s‘,strtotime(‘last day of -1 month‘)) , "\n";//2019-12-31 10:39:12 echo ‘上个月最后一天零点:‘ , date(‘Y-m-d H:i:s‘, strtotime("midnight last day of -1 month")) , "\n"; //2019-12-31 00:00:00 echo ‘昨天零点:‘ , date(‘Y-m-d H:i:s‘,strtotime(‘yesterday‘)) , "\n";//2020-01-09 00:00:00 echo ‘现在:‘ , date(‘Y-m-d H:i:s‘,strtotime(‘now‘)) , "\n";//2020-01-10 10:39:12 echo ‘三个星期之间的时间戳是:‘ , strtotime(‘-3 weeks‘);//三个星期之间的时间戳是:1576810790 echo (time() - strtotime(‘-3 weeks‘))/86400 ;//21 间隔时间 echo ‘上个月:‘.date(‘Y-m-d H:i:s‘,strtotime(‘-1 month‘)) ; //上个月:2019-12-10 10:59:50 echo ‘上个月的第一天:‘.date(‘Y-m-d H:i:s‘,strtotime(‘first day of -1 month‘));//上个月的第一天:2019-12-01 10:59:50
返回当前本地的日期/时间的日期/时间信息
print_r(getdate()); //Array //( // [seconds] => 3 // [minutes] => 42 // [hours] => 10 // [mday] => 10 // [wday] => 5 // [mon] => 1 // [year] => 2020 // [yday] => 9 // [weekday] => Friday //[month] => January //[0] => 1578624123 //)
microtime()返回当前 Unix 时间戳的微秒数
echo microtime();//0.41369400 1578624195
当设置为 TRUE 时,规定函数应该返回一个浮点数,否则返回一个字符串;默认为 FALSE
echo microtime(true);//1578624195.4137
计算程序运行时间
$start = microtime(true); $sum = 0; for ($i=0; $i <1000000 ; $i++) { $sum += $i; } $end = microtime(true); echo ‘共花费‘ , round($end - $start,3) , ‘秒‘;//共花费0.016秒
time()
echo time() ;//1578625294 echo ‘当前的日期时间是:‘ , date(‘Y-m-d H:i:s‘) ;//当前的日期时间是:2020-01-10 11:01:34 echo ‘昨天的日期时间是:‘ , date(‘Y-m-d H:i:s‘,time()-86400) ; //24*60*60 //昨天的日期时间是:2020-01-09 11:01:34
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID
echo uniqid();//5e17e94f8a19b echo uniqid(‘abc‘);//abc5e17e96c1771e echo uniqid(microtime());//0.09603300 15786253885e17e96c17727 echo uniqid(microtime() . mt_rand()); //mt_rand(100,999);//0.09604200 15786253884744704985e17e96c1772f //uuid 8-4-4-4-12 = 32 echo md5(uniqid(microtime() . mt_rand()));//cf6333288fcb04f60fbbedafd127201e
session
session_start(); echo session_id();//bp99jhu204h6vi214ttgcjce80
相关推荐
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh