我們可以使用lint檢查
用lint靜態(tài)檢查。
靜態(tài)是一種只檢查語法描述方法而不執(zhí)行程序的方法。
此時使用lint命令。
※php_check_syntax這個語法檢查函數(shù)已經(jīng)被廢止,所以不能使用。
然后準備實際出現(xiàn)錯誤的php文件。
lint_test.php
?php echo "error"
它只是一個在屏幕上顯示error的代碼。
將lint_test.php移動到某個目錄并發(fā)出以下命令。
php -l lint_test.php
執(zhí)行結果
PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in lint_test.php on line 2 Parse error: syntax error, unexpected end of file, expecting ',' or ';' in lint_test.php on line 2Errors parsing lint_test.php
syntax error=輸出語法錯誤指示。
它還返回錯誤行數(shù)為line 2。
因為有unexpected end of file,是第2行沒有“;”是原因。
那么,修改lint_test.php,再次執(zhí)行l(wèi)int命令。
?php echo "error";
執(zhí)行結果為:
No syntax errors detected in lint_test.php
顯示沒有語法錯誤。
使用xdebug動態(tài)檢查語法錯誤
首先,啟用xdebug。
①從下面的官方站點下載xdebug,并記下下載的.dll文件所在的本地環(huán)境的路徑。
https://xdebug.org/download.php
② 將以下內容添加到php.ini中。
zend_extension = ①中記錄的路徑
②重啟Web服務器(Apache等)
這樣就完成了設置。
使用xdebug檢查錯誤
我們運行上述使用的lint_test.php。
lint_test.php
?php echo "error"
有一個錯誤,因為最后沒有分號。
內容與執(zhí)行l(wèi)int時的內容相同,但附加了一些裝飾以便于查看。
與lint的最大區(qū)別在于執(zhí)行代碼后出現(xiàn)的錯誤,因此可以說由于動態(tài)檢查而出現(xiàn)錯誤。