九九热这里直有精品,1区二区三区在线播放,玖玖爱在线观看资源,国产aⅴ综合网,午夜福利男女,日本亚洲欧美三级,日韩无码黄色导航,内射少妇13区,中文字幕高清网

您身邊的軟件定制專家--9年開發(fā)經(jīng)驗為您護航

18678812288
0531-88887250

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

文章作者:濟南軟件開發(fā) 時間:2016年11月08日

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

  get_defined_vars()

  這是一個在debug調(diào)試時非常有用的函數(shù)。這個函數(shù)返回一個多維數(shù)組,里面包含了所有定義過的變量。

  用法:

  <?php

  print_r(get_defined_vars());

  ?>

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

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

  ignore_user_abort()

  這個函數(shù)用來拒絕瀏覽器端用戶終止執(zhí)行腳本的請求。正常情況下客戶端的退出會導致服務(wù)器端腳本停止運行。

  用法:

  <?php

  ignore_user_abort();

  ?>

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

  highlight_string()

  當你想把PHP代碼顯示到頁面上時,highlight_string()函數(shù)就會顯得非常有用。這個函數(shù)會把你提供的PHP代碼用內(nèi)置的PHP語法突出顯示定義的顏色高亮顯示。這個函數(shù)有兩個參數(shù),第一個參數(shù)是一個字符串,表示這個字符串需要被突出顯示。第二個參數(shù)如果設(shè)置成TRUE,這個函數(shù)就會把高亮后的代碼當成返回值返回。

  用法

  <?php

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

  ?>

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

  highlight_file

  這是一個非常有用的PHP函數(shù),它能返回指定的PHP文件,并按照語法語義用高亮顏色突出顯示文件內(nèi)容。其中的突出顯示的代碼都是用HTML標記處理過的。

  用法:

  <?php

  highlight_file(“php_script.php”);

  ?>

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

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

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

  memory_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ù)用來壓縮和解壓字符串數(shù)據(jù)。它們的壓縮率能達到50% 左右。另外的函數(shù) gzencode() 和 gzdecode() 也能達到類似結(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ù)?請在評論里分享出來!


想要了解更多詳情歡迎來電咨詢18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。

珲春市| 修武县| 卓资县| 武穴市| 高尔夫| 玛纳斯县| 湛江市| 库车县| 通河县| 石门县| 奉贤区| 进贤县| 元朗区| 安图县| 金昌市| 抚松县| 澜沧| 图们市| 龙川县| 淅川县| 元阳县| 沙雅县| 邹平县| 苍溪县| 左贡县| 青神县| 吉安市| 丰都县| 密山市| 贵州省| 台南县| 鹤壁市| 临潭县| 台南市| 荔波县| 托克逊县| 青田县| 威信县| 西藏| 水城县| 固镇县|