PHP培訓教程 PHP裡10個鮮為人知但卻非常有用的函數

來源:互聯網
上載者:User

標籤:兄弟連   php   php培訓   

PHP培訓教程 PHP裡10個鮮為人知但卻非常有用的函數

php裡有非常豐富的內建函數,很多我們都用過,但仍有很多的函數我們大部分人都不熟悉,可它們卻十分的有用。這篇文章裡,兄弟連小編列舉了一些PHP培訓鮮為人知但會讓你眼睛一亮的PHP函數。

levenshtein()

  你有沒有經曆過需要知道兩個單詞有多大的不同的時候,這個函數就是來幫你解決這個問題的。它能比較出兩個字串的不同程度。

  用法:

<?php

$str1 = "carrot";

$str2 = "carrrott";

echo levenshtein($str1, $str2); //Outputs 2

?>

Source: http://php.net/manual/en/function.levenshtein.php

get_defined_vars()

  這是一個在debug調試時非常有用的函數。這個函數返回一個多維陣列,裡麵包含了所有定義過的變數。

  用法:

<?php

print_r(get_defined_vars());

?>

Source: http://php.net/manual/en/function.get-defined-vars.php

php_check_syntax()

  這個函數非常的有用,可以用來檢查PHP的文法是否正確。出於技術上的原因,從PHP 5.05開始,這個函數被刪除了。

  用法:

<?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.php

ignore_user_abort()

  這個函數用來拒絕瀏覽器端使用者終止執行指令碼的請求。正常情況下用戶端的退出會導致伺服器端指令碼停止運行。

  用法:

<?php

ignore_user_abort();

?>

Source: http://www.php.net/manual/en/function.ignore-user-abort.php

highlight_string()

  當你想把PHP代碼顯示到頁面上時,highlight_string()函數就會顯得非常有用。這個函數會把你提供的PHP代碼用內建的PHP文法反白定義的顏色高亮顯示。這個函數有兩個參數,第一個參數是一個字串,表示這個字串需要被反白。第二個參數如果設定成TRUE,這個函數就會把高亮後的代碼當成傳回值返回。

  用法

<?php

highlight_string(‘ <?php phpinfo(); ?>‘);

?>

Source: http://php.net/manual/en/function.highlight-string.php

highlight_file

  這是一個非常有用的PHP函數,它能返回指定的PHP檔案,並按照文法語義用醒目提示反白檔案內容。其中的反白的代碼都是用HTML標記處理過的。

  用法:

<?php

highlight_file("php_script.php");

?>

Source: http://www.php.net/manual/en/function.highlight-file.php

php_strip_whitespace

  這個函數也跟前面的show_source()函數相似,但它會刪除檔案裡的注釋和空格符。

  用法:

<?php

echo php_strip_whitespace("php_script.php");

?>

Source: http://www.php.net/manual/en/function.php-strip-whitespace.php

get_browser

  這個函數會讀取browscap.ini檔案,返回瀏覽器安全色資訊。

  用法:

<?php

echo $_SERVER[‘HTTP_USER_AGENT‘];

$browser = get_browser();

print_r($browser);

?>

Source: http://www.php.net/manual/en/function.get-browser.php

memory_get_usage(),memory_get_peak_usage(),getrusage()

  這些函數用來擷取記憶體和CPU使用方式,memory_get_usage()函數返回記憶體使用量量,memory_get_peak_usage()函數返回尖峰記憶體使用量,getrusage()返回CUP使用方式,在調試PHP代碼效能時,這些函數會給你提供一些有用資訊。但有一點請注意,在這些函數中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()

  這兩個函數用來壓縮和解壓字串資料。它們的壓縮率能達到50% 左右。另外的函數 gzencode() 和 gzdecode() 也能達到類似結果,但使用了不同的壓縮演算法。

  用法:

<?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);

?>


PHP培訓教程 PHP裡10個鮮為人知但卻非常有用的函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.