php系统函数代码,PHP自定义函数+系统函数库(代码示例)
全局變量$n = 5; //全局變量
function fun1(){
global $n;
echo '我在函數體內也可以調用全局變量n,它的值是:' , $n;//5
$n++;
}
fun1();
echo '
';
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 , '
';//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)) , '
';//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
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 = 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_Frontfloor() ceil()
$x = 2.7;
$y = 3.01;
echo floor($x) , '
';//2 向下取整
echo ceil($y) , '
';//4 向上取整
假設記錄數為X,每頁顯示Y條記錄,求總頁數zz = ceil(X/Y);
fmod()對浮點數取模echo fmod(7.8,3) , '
';//1.8
對整數取模echo 7.8 % 3 ; //整數余數的操作//1
格式化數字$x = 7896.827;
echo number_format($x) , '
';//7,897
echo number_format($x,2) , '
';//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 時,規定函數應該返回一個浮點數,否則返回一個字符串;默認為 FALSEecho 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() 函數基于以微秒計的當前時間,生成一個唯一的 IDecho 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()));//cf6333288fcb04f60fbbedafd127201esession
session_start();
echo session_id();//bp99jhu204h6vi214ttgcjce80
更多相關php知識,請訪問php教程!
總結
以上是生活随笔為你收集整理的php系统函数代码,PHP自定义函数+系统函数库(代码示例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 索尼新款 PS5 主机同捆包曝光:《战神
- 下一篇: config database.php,