PHP高級函數(shù)
1、call_user_func
// 官網(wǎng)地址: http://php.com/manual/zh/function.call-user-func.php
2、get_class
// 官網(wǎng)地址: http://php.com/manual/zh/function.get-class.php
3、get_called_class
// 官網(wǎng)地址: http://php.com/manual/zh/function.get-called-class.php
4、array_map
// 官網(wǎng)地址:http://php.com/manual/zh/function.array-map.php //為數(shù)組的每個元素應(yīng)用回調(diào)函數(shù) 示例: $str = '1 ,2,3'; $res = array_map(function ($v) { return intval(trim($v)) * 2; }, explode(',', $str)); $res的返回結(jié)果: array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(6) }
5、strpos
// http://php.com/manual/zh/function.strpos.php //查找字符串首次出現(xiàn)的位置,從0開始編碼,沒有找到返回false 示例: $time = "2019-03-02 12:00:00"; if(strpos($time,':') !== false){ $time = strtotime($time); } echo $time;
6、array_reverse
// 官網(wǎng)地址:http://php.com/manual/zh/function.array-reverse.php //返回單元順序相反的數(shù)組 示例: $time = "12:13:14"; $arrTime = array_reverse(explode(':',$time)); var_dump($arrTime); array(3) { [0]=> string(2) "14" [1]=> string(2) "13" [2]=> string(2) "12" }
7、pow
// 官網(wǎng)地址:http://php.com/manual/zh/function.pow.php //指數(shù)表達(dá)式 示例: $time = "12:13:14"; $arrTime = array_reverse(explode(':',$time)); $i = $s = 0; foreach($arrTime as $time){ $s += $time * pow(60,$i); // 60 的 $i 次方 $i ++; } var_dump($s); int(43994)
8、property_exist
// 官網(wǎng)地址:http://php.com/manual/zh/function.property-exists.php // 檢查對象或類是否具有該屬性 //如果該屬性存在則返回 TRUE,如果不存在則返回 FALSE,出錯返回 NULL 示例: class Test { public $name = 'daicr'; public function index() { var_dump(property_exists($this,'name')); // true } }
9、passthru
// 官網(wǎng)地址:http://php.com/manual/zh/function.passthru.php //執(zhí)行外部程序并且顯示原始輸出 //功能和exec() system() 有類似之處 示例: passthru(Yii::$app->basePath.DIRECTORY_SEPARATOR . 'yii test/index');
10、array_filter
// 官網(wǎng)地址:http://php.com/manual/zh/function.array-filter.php //用回調(diào)函數(shù)過濾數(shù)組中的單元 示例: class TestController extends yiiconsoleController { public $modules = ''; public function actionIndex() { //當(dāng)不使用callBack函數(shù)時,array_filter會去除空值或者false $enableModules = array_filter(explode(',',$this->modules)); var_dump(empty($enableModules)); //true //當(dāng)使用callBack函數(shù)時,就會用callBack過濾數(shù)組中的單元 $arr = [1,2,3,4]; $res = array_filter($arr,function($v){ return $v & 1; //先轉(zhuǎn)換為二進(jìn)制,在按位進(jìn)行與運(yùn)算,得到奇數(shù) }); var_dump($res); //array(2) { [0]=> int(1) [2]=> int(3) } } }
11、current
// 官網(wǎng)地址:http://php.com/manual/zh/function.current.php //返回數(shù)組中的當(dāng)前單元 $arr = ['car'=>'BMW','bicycle','airplane']; $str1 = current($arr); //初始指向插入到數(shù)組中的第一個單元。 $str2 = next($arr); //將數(shù)組中的內(nèi)部指針向前移動一位 $str3 = current($arr); //指針指向它“當(dāng)前的”單元 $str4 = prev($arr); //將數(shù)組的內(nèi)部指針倒回一位 $str5 = end($arr); //將數(shù)組的內(nèi)部指針指向最后一個單元 reset($arr); //將數(shù)組的內(nèi)部指針指向第一個單元 $str6 = current($arr); $key1 = key($arr); //從關(guān)聯(lián)數(shù)組中取得鍵名 echo $str1 . PHP_EOL; //BMW echo $str2 . PHP_EOL; //bicycle echo $str3 . PHP_EOL; //bicycle echo $str4 . PHP_EOL; //BMW echo $str5 . PHP_EOL; //airplane echo $str6 . PHP_EOL; //BMW echo $key1 . PHP_EOL; //car var_dump($arr); //原數(shù)組不變
12、array_slice
// 官網(wǎng)地址:http://php.com/manual/zh/function.array-slice.php //從數(shù)組中取出一段 示例: $idSet = [1,2,3,4,5,6,7,8,9,10]; $total = count($idSet); $offset = 0; $success = 0; while ($offset < $total){ $arrId = array_slice($idSet,$offset,5); //yii2的語法,此處,注意array_slice的用法就行 $success += $db->createCommand()->update($table,['sync_complate'=>1],['id'=>$arrId])->execute(); $offset += 50; } $this->stdout('共:' . $total . ' 條,成功:' . $success . ' 條' . PHP_EOL,Console::FG_GREEN); //yii2的語法
13、mb_strlen()
// 官網(wǎng)地址:http://php.com/manual/zh/function.mb-strlen.php //獲取字符串的長度 //strlen 獲取的是英文字節(jié)的字符長度,而mb_stren可以按編碼獲取中文字符的長度 示例: $str1 = 'daishu'; $str2 = '袋鼠'; echo strlen($str1) . PHP_EOL; //6 echo mb_strlen($str1,'utf-8') . PHP_EOL; //6 echo strlen($str2) . PHP_EOL; // 4 一個中文占 2 個字節(jié) echo mb_strlen($str2,'utf-8') . PHP_EOL; //2 echo mb_strlen($str2,'gb2312') . PHP_EOL; //2
14、list
// 官網(wǎng)地址:http://php.com/manual/zh/function.list.php //把數(shù)組中的值賦給一組變量 示例: list($access,$department)= ['all','1,2,3']; var_dump($access); // all
15、strcasecmp
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.strcasecmp.php //二進(jìn)制安全比較字符串(不區(qū)分大小寫) //如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果兩者相等,返回 0。 示例: $str1 = 'chrdai'; $str2 = 'chrdai'; var_dump(strcasecmp($str1,$str2)); // int 0
16、fopen rb
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.fopen.php //1、使用 'b' 來強(qiáng)制使用二進(jìn)制模式,這樣就不會轉(zhuǎn)換數(shù)據(jù),規(guī)避了widown和unix換行符不通導(dǎo)致的問題, //2、還有就是在操作二進(jìn)制文件時如果沒有指定'b'標(biāo)記,可能會碰到一些奇怪的問題,包括壞掉的圖片文件以及關(guān)于 字符的奇怪問題。 示例: $handle = fopen($filePath, 'rb');
17、fseek
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.fseek.php //在文件指針中定位 //必須是在一個已經(jīng)打開的文件流里面,指針位置為:第三個參數(shù) + 第二個參數(shù) 示例: //將文件指針移動到文件末尾 SEEK_END + 0 fseek($handle, 0, SEEK_END);
18、ftell
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.ftell.php //返回文件指針讀/寫的位置 //如果將文件的指針用fseek移動到文件末尾,在用ftell讀取指針位置,則指針位置即為文件大小。 示例: //將文件指針移動到文件末尾 SEEK_END + 0 fseek($handle, 0, SEEK_END); //此時文件大小就等于指針的偏移量 $fileSize = ftell($handle);
19、basename
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.basename.php //返回路徑中的文件名部分 示例: echo basename('/etc/sudoers.d'); // sudoers ,注意沒有文件的后綴名,和pathinfo($filePath)['filename']功能差不多
20、pathinfo
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.pathinfo.php //返回文件路徑的信息 示例: $pathParts = pathinfo('/etc/php.ini'); echo $pathParts['dirname'] . PHP_EOL; // /etc ,返回路徑信息中的目錄部分 echo $pathParts['basename'] . PHP_EOL; // php.ini ,包括文件名和拓展名 echo $pathParts['extension'] . PHP_EOL; // ini ,拓展名 echo $pathParts['filename'] . PHP_EOL; // php ,只有文件名,不包含拓展名 ,和basename()函數(shù)功能差不多
21、headers_sent($file, $line)
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.headers-sent.php //檢測 HTTP 頭是否已經(jīng)發(fā)送 //1、http頭已經(jīng)發(fā)送時,就無法通過header()函數(shù)添加更多頭信息,使用次函數(shù)起碼可以防止HTTP頭出錯 //2、可選參數(shù)$file和$line不需要先定義,如果設(shè)置了這兩個值,headers_sent()會把文件名放在$file變量,把輸出開始的行號放在$line變量里
22、header('$name: $value', $replace)
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.header.php //發(fā)送原生 HTTP 頭 //1、注意:header必須在所有實際輸出之前調(diào)用才能生效。 //2、header的$replace參數(shù)默認(rèn)為true,會自動用后面的替換前面相同的頭信息,如果設(shè)為false,則強(qiáng)制使相同的頭信息并存 示例: public function sendHeader() { if (headers_sent($file, $line)) { throw new Exception("Headers already sent in {$file} on line {$line}"); } $headers = [ 'Content-Type' => [ 'application/octet-stream', 'application/force-download', ], 'Content-Disposition' => [ 'attachment;filename=test.txt', ], ]; foreach($headers as $name => $values) { //所有的http報頭的名稱都是首字母大寫,且多個單詞以 - 分隔 $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name))); $replace = true; foreach($values as $value) { header("$name: $value", $replace); $replace = false; //強(qiáng)制使相同的頭信息并存 } } }
22、array_multisort($array1, SORT_ASC|SORT_DESC, $array2)
// 官網(wǎng)地址:https://www.php.com/manual/zh/function.array-multisort.php // 對多個數(shù)組或多維數(shù)組進(jìn)行排序 //說明: $array1 : 排序結(jié)果是所有的數(shù)組都按第一個數(shù)組的順序進(jìn)行排列 // $array2 : 待排序的數(shù)組
示例:
$array2 = [ 1000 => [ 'name' => '張三', 'age' => 25, ], 1001 => [ 'name' => '李四', 'age' => 26, ], ]; //如果想將 $array2 按照 age 進(jìn)行排序。 //不過需要注意的是:兩個數(shù)組的元素個數(shù)必須相同,不然就會出現(xiàn)一個警告信息: //Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in …… //第一步:將age的數(shù)據(jù)拿出來作為一個單獨的數(shù)組,作為排序的依據(jù)。 $array1 = []; foreach ($array2 as $key => $val) { array_push($array1, $val['age']); } //第二步驟:使用 array_multisort() 進(jìn)行排序。 array_multisort($array1, SORT_DESC, $array2); var_dump($array2); //數(shù)組的健名字如果是數(shù)字會被重置,字符串不會 // array (size=2) // 0 => // array (size=2) // 'name' => string '李四' (length=6) // 'age' => int 26 // 1 => // array (size=2) // 'name' => string '張三' (length=6) // 'age' => int 2
23、strtr 轉(zhuǎn)換指定字符串
//官網(wǎng)文檔:https://www.php.com/manual/zh/function.strtr.php strtr(string $str , string $from , string $to ) strtr ( string $str , array $replace_pairs ) //例如: $str = "<div class='just-sm-6 just-md-6'><div class='control_text'>{label}<font>*</font></div></div> <div class='just-sm-18 just-md-18'><div class='control_element'>{input} {hint} {error}</div></div>"; $parts = [ '{label}' => '年齡', '{input}' => '<input name="age" id="user-age" class="inputs" value="" />', '{hint}' => '年齡必須是 0-200 直接的數(shù)字', '{error}' => '格式不正確', ]; $string = strtr($str, $parts); echo htmlspecialchars($string); //<div class='just-sm-6 just-md-6'><div class='control_text'>年齡<font>*</font></div></div> <div class='just-sm-18 just-md-18'><div class='control_element'><input name="age" id="user-age" class="inputs" value="" /> 年齡必須是 0-200 直接的數(shù)字 格式不正確</div></div> var_dump(Yii::getAlias('@webroot')); var_dump(Yii::getAlias('@web'));
24、ReflectionClass 報告類的有關(guān)信息
//ReflectionClass 報告了一個類的有關(guān)信息 //官網(wǎng)地址:https://www.php.com/manual/zh/class.reflectionclass.php //例如: $class = new ReflectionClass($this); //打印當(dāng)前類文件所在目錄 var_dump(dirname($class->getFileName())); //var/www/html/basic/controllers
25、call_user_func_array 調(diào)用回調(diào)函數(shù),并把一個數(shù)組參數(shù)作為回調(diào)函數(shù)的參數(shù)
//官網(wǎng)地址:https://www.php.com/manual/zh/function.call-user-func-array.php function foobar($arg, $arg2) { echo __FUNCTION__, " got $arg and $arg2 "; } call_user_func_array("foobar", array("one", "two")); //輸出結(jié)果: foobar got one and two
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com