php一些高级函数方法
生活随笔
收集整理的這篇文章主要介紹了
php一些高级函数方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?PHP高級函數
1、call_user_func (http://php.net/manual/zh/function.call-user-func.php)2、get_class (http://php.net/manual/zh/function.get-class.php)3、get_called_class (http://php.net/manual/zh/function.get-called-class.php)?
4、array_map http://php.net/manual/zh/function.array-map.php //為數組的每個元素應用回調函數 示例: $str = '1 ,2,3'; $res = array_map(function ($v) {return intval(trim($v)) * 2; }, explode(',', $str));$res的返回結果: array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(6) }?
5、strpos http://php.net/manual/zh/function.strpos.php //查找字符串首次出現的位置,從0開始編碼,沒有找到返回false 示例: $time = "2019-03-02 12:00:00"; if(strpos($time,':') !== false){$time = strtotime($time); } echo $time;?
6、array_reverse http://php.net/manual/zh/function.array-reverse.php //返回單元順序相反的數組 示例: $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 http://php.net/manual/zh/function.pow.php //指數表達式 示例: $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 http://php.net/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 http://php.net/manual/zh/function.passthru.php //執行外部程序并且顯示原始輸出 //功能和exec() system() 有類似之處 示例: passthru(\Yii::$app->basePath.DIRECTORY_SEPARATOR . 'yii test/index');?
10、array_filter http://php.net/manual/zh/function.array-filter.php //用回調函數過濾數組中的單元 示例: class TestController extends yii\console\Controller {public $modules = '';public function actionIndex(){//當不使用callBack函數時,array_filter會去除空值或者false$enableModules = array_filter(explode(',',$this->modules));var_dump(empty($enableModules)); //true//當使用callBack函數時,就會用callBack過濾數組中的單元$arr = [1,2,3,4];$res = array_filter($arr,function($v){return $v & 1; //先轉換為二進制,在按位進行與運算,得到奇數 });var_dump($res);//array(2) { [0]=> int(1) [2]=> int(3) } } }·
11、current http://php.net/manual/zh/function.current.php //返回數組中的當前單元$arr = ['car'=>'BMW','bicycle','airplane']; $str1 = current($arr); //初始指向插入到數組中的第一個單元。 $str2 = next($arr); //將數組中的內部指針向前移動一位 $str3 = current($arr); //指針指向它“當前的”單元 $str4 = prev($arr); //將數組的內部指針倒回一位 $str5 = end($arr); //將數組的內部指針指向最后一個單元 reset($arr); //將數組的內部指針指向第一個單元 $str6 = current($arr); $key1 = key($arr); //從關聯數組中取得鍵名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); //原數組不變?12、array_slice http://php.net/manual/zh/function.array-slice.php
//從數組中取出一段 示例: $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() http://php.net/manual/zh/function.mb-strlen.php //獲取字符串的長度 //strlen 獲取的是英文字節的字符長度,而mb_stren可以按編碼獲取中文字符的長度 示例: $str1 = 'daishu'; $str2 = '袋鼠'; echo strlen($str1) . PHP_EOL; //6 echo mb_strlen($str1,'utf-8') . PHP_EOL; //6echo strlen($str2) . PHP_EOL; // 4 一個中文占 2 個字節 echo mb_strlen($str2,'utf-8') . PHP_EOL; //2 echo mb_strlen($str2,'gb2312') . PHP_EOL; //2?14、list http://php.net/manual/zh/function.list.php
//把數組中的值賦給一組變量 示例: list($access,$department)= ['all','1,2,3']; var_dump($access); // all?
15、strcasecmp https://www.php.net/manual/zh/function.strcasecmp.php //二進制安全比較字符串(不區分大小寫) //如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果兩者相等,返回 0。 示例: $str1 = 'chrdai'; $str2 = 'chrdai'; var_dump(strcasecmp($str1,$str2)); // int 0?
16、fopen rb https://www.php.net/manual/zh/function.fopen.php//1、使用 'b' 來強制使用二進制模式,這樣就不會轉換數據,規避了widown和unix換行符不通導致的問題, //2、還有就是在操作二進制文件時如果沒有指定'b'標記,可能會碰到一些奇怪的問題,包括壞掉的圖片文件以及關于\r\n 字符的奇怪問題。 示例: $handle = fopen($filePath, 'rb');?
17、fseek https://www.php.net/manual/zh/function.fseek.php //在文件指針中定位 //必須是在一個已經打開的文件流里面,指針位置為:第三個參數 + 第二個參數 示例: //將文件指針移動到文件末尾 SEEK_END + 0 fseek($handle, 0, SEEK_END);?
18、ftell https://www.php.net/manual/zh/function.ftell.php //返回文件指針讀/寫的位置//如果將文件的指針用fseek移動到文件末尾,在用ftell讀取指針位置,則指針位置即為文件大小。 示例: //將文件指針移動到文件末尾 SEEK_END + 0 fseek($handle, 0, SEEK_END); //此時文件大小就等于指針的偏移量 $fileSize = ftell($handle);?
19、basename https://www.php.net/manual/zh/function.basename.php //返回路徑中的文件名部分 示例: echo basename('/etc/sudoers.d'); // sudoers ,注意沒有文件的后綴名,和pathinfo($filePath)['filename']功能差不多?
20、pathinfo https://www.php.net/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()函數功能差不多?
21、headers_sent($file, $line) https://www.php.net/manual/zh/function.headers-sent.php //檢測 HTTP 頭是否已經發送//1、http頭已經發送時,就無法通過header()函數添加更多頭信息,使用次函數起碼可以防止HTTP頭出錯 //2、可選參數$file和$line不需要先定義,如果設置了這兩個值,headers_sent()會把文件名放在$file變量,把輸出開始的行號放在$line變量里22、header('$name: $value', $replace) https://www.php.net/manual/zh/function.header.php //發送原生 HTTP 頭//1、注意:header必須在所有實際輸出之前調用才能生效。 //2、header的$replace參數默認為true,會自動用后面的替換前面相同的頭信息,如果設為false,則強制使相同的頭信息并存 示例: 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; //強制使相同的頭信息并存 }} }?
22、array_multisort($array1, SORT_ASC|SORT_DESC, $array2) https://www.php.net/manual/zh/function.array-multisort.php // 對多個數組或多維數組進行排序 //說明: $array1 : 排序結果是所有的數組都按第一個數組的順序進行排列 // $array2 : 待排序的數組 示例: $array2 = [1000 => ['name' => '張三','age' => 25,],1001 => ['name' => '李四','age' => 26,], ]; //如果想將 $array2 按照 age 進行排序。 //不過需要注意的是:兩個數組的元素個數必須相同,不然就會出現一個警告信息: //Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in …… //第一步:將age的數據拿出來作為一個單獨的數組,作為排序的依據。 $array1 = []; foreach ($array2 as $key => $val) {array_push($array1, $val['age']); } //第二步驟:使用 array_multisort() 進行排序。 array_multisort($array1, SORT_DESC, $array2); var_dump($array2); //數組的健名字如果是數字會被重置,字符串不會 // 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? 轉換指定字符串
23、strtr // 轉換指定字符 //官網文檔:https://www.php.net/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 直接的數字','{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 直接的數字 格式不正確</div></div>var_dump(Yii::getAlias('@webroot')); var_dump(Yii::getAlias('@web'));?24、ReflectionClass? 報告類的有關信息
//ReflectionClass 報告了一個類的有關信息 //官網地址:https://www.php.net/manual/zh/class.reflectionclass.php //例如: $class = new \ReflectionClass($this); //打印當前類文件所在目錄 var_dump(dirname($class->getFileName())); //var/www/html/basic/controllers?
?
?
`
?
轉載于:https://www.cnblogs.com/chrdai/p/10447837.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的php一些高级函数方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv vs2010 配置
- 下一篇: WD与循环 组合数学