成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

PHP里10個鮮為人知但卻非常有用的函數(shù)

瀏覽:6日期:2022-09-14 08:59:46
PHP里有非常豐富的內(nèi)置函數(shù),很多我們都用過,但仍有很多的函數(shù)我們大部分人都不熟悉,可它們卻十分的有用。這篇文章里,我列舉了一些鮮為人知但會讓你眼睛一亮的PHP函數(shù)。levenshtein()你有沒有經(jīng)歷過需要知道兩個單詞有多大的不同的時候,這個函數(shù)就是來幫你解決這個問題的。它能比較出兩個字符串的不同程度。用法:<?php $str1 = 'carrot'; $str2 = 'carrrott'; echo levenshtein($str1, $str2); //Outputs 2 ?> Source: http://php.net/manual/en/function.levenshtein.phpget_defined_vars()這是一個在debug調(diào)試時非常有用的函數(shù)。這個函數(shù)返回一個多維數(shù)組,里面包含了所有定義過的變量。用法:<?php print_r(get_defined_vars()); ?> Source: http://php.net/manual/en/function.get-defined-vars.phpphp_check_syntax()這個函數(shù)非常的有用,可以用來檢查PHP的語法是否正確。出于技術(shù)上的原因,從PHP 5.05開始,這個函數(shù)被刪除了。用法:<?php $error_message = ''; $filename = './php_script.php'; if(!php_check_syntax($filename, &$error_message)) { echo 'Errors were found in the file $filename: $error_message'; } else { echo 'The file $filename contained no syntax errors'; } ?> Source: http://www.php.net/manual/en/function.php-check-syntax.phpignore_user_abort()這個函數(shù)用來拒絕瀏覽器端用戶終止執(zhí)行腳本的請求。正常情況下客戶端的退出會導(dǎo)致服務(wù)器端腳本停止運(yùn)行。用法:<?php ignore_user_abort(); ?> Source: http://www.php.net/manual/en/function.ignore-user-abort.phphighlight_string()當(dāng)你想把PHP代碼顯示到頁面上時,highlight_string()函數(shù)就會顯得非常有用。這個函數(shù)會把你提供的PHP代碼用內(nèi)置的PHP語法突出顯示定義的顏色高亮顯示。這個函數(shù)有兩個參數(shù),第一個參數(shù)是一個字符串,表示這個字符串需要被突出顯示。第二個參數(shù)如果設(shè)置成TRUE,這個函數(shù)就會把高亮后的代碼當(dāng)成返回值返回。用法 <?php highlight_string(’ <?php phpinfo(); ?>’); ?> Source: http://php.net/manual/en/function.highlight-string.phphighlight_file這是一個非常有用的PHP函數(shù),它能返回指定的PHP文件,并按照語法語義用高亮顏色突出顯示文件內(nèi)容。其中的突出顯示的代碼都是用HTML標(biāo)記處理過的。用法:<?php highlight_file('php_script.php'); ?> Source: http://www.php.net/manual/en/function.highlight-file.phpphp_strip_whitespace這個函數(shù)也跟前面的show_source()函數(shù)相似,但它會刪除文件里的注釋和空格符。用法:<?php echo php_strip_whitespace('php_script.php'); ?> Source: http://www.php.net/manual/en/function.php-strip-whitespace.phpget_browser這個函數(shù)會讀取browscap.ini文件,返回瀏覽器兼容信息。用法:<?php echo $_SERVER[’HTTP_USER_AGENT’]; $browser = get_browser(); print_r($browser); ?> Source: http://www.php.net/manual/en/function.get-browser.phpmemory_get_usage(),memory_get_peak_usage(),getrusage()這些函數(shù)用來獲取內(nèi)存和CPU使用情況,memory_get_usage()函數(shù)返回內(nèi)存使用量,memory_get_peak_usage()函數(shù)返回內(nèi)存使用峰值,getrusage()返回CUP使用情況,在調(diào)試PHP代碼性能時,這些函數(shù)會給你提供一些有用信息。但有一點請注意,在這些函數(shù)中Window上無效。用法: <?php echo 'Initial: '.memory_get_usage().' bytes n'; echo 'Peak: '.memory_get_peak_usage().' bytes n'; $data = getrusage(); echo 'User time: '. ($data[’ru_utime.tv_sec’] + $data[’ru_utime.tv_usec’] / 1000000); echo 'System time: '. ($data[’ru_stime.tv_sec’] + $data[’ru_stime.tv_usec’] / 1000000); ?> gzcompress(), gzuncompress()這兩個函數(shù)用來壓縮和解壓字符串?dāng)?shù)據(jù)。它們的壓縮率能達(dá)到50% 左右。另外的函數(shù) gzencode() 和 gzdecode() 也能達(dá)到類似結(jié)果,但使用了不同的壓縮算法。用法:<?php $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum, nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac massa sed turpis tempor luctus. Curabitur sed nibh eu elit mollis congue. Praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in, mollis sed nunc. Etiam at justo in leo congue mollis. Nullam in neque eget metus hendrerit scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. '; $compressed = gzcompress($string); $original = gzuncompress($compressed); ?> 你是否也想到了還有其它很有用的函數(shù)?請在評論里分享出來!原文鏈接:http://www.phpzag.com/10-little-known-but-useful-php-functions/譯文鏈接:http://www.aqee.net/10-little-known-but-useful-php-functions/
標(biāo)簽: PHP
相關(guān)文章: