搜尋
首頁後端開發php教程php裡10個鮮為人知但卻非常有用的函數

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 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()

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

用法

<?php 
highlight_string(&#39; <?php phpinfo(); ?>&#39;); 
?>

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
會刪除文件裡的註解和空格符。

用法:

<?php 
echo php_strip_whitespace("php_script.php"); 
?>

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


get_browser

這個函數會讀取wsbrocap.器相容資訊。

用法:

<?php 
echo $_SERVER[&#39;HTTP_USER_AGENT&#39;]; 
$browser = get_browser(); 
print_r($browser); 
?>

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


memory_get_usage(),memory_get_peak_usage(memory_get_usage(),memory_get_peak_usage(rus]取得記憶體和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[&#39;ru_utime.tv_sec&#39;] + 
    $data[&#39;ru_utime.tv_usec&#39;] / 1000000); 
echo "System time: ". 
    ($data[&#39;ru_stime.tv_sec&#39;] + 
    $data[&#39;ru_stime.tv_usec&#39;] / 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); 
 
?>

你是否也想到了還有其它很有用的函數?請在評論裡分享出來!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器