Troubleshooting of PHP errors

From LemonWiki共筆
Jump to navigation Jump to search

檔案操作有關


使用者操作/使用者輸入的內容

使用者輸入內容,內容可能是

  • 空值
  • 包含特殊符號 ex: 單引號 ', 雙引號 ", > < <== 對策: PHP: htmlentities - Manual
  • 包含換行符號
  • 內容的前後有空白 <== 對策: PHP: trim 或 jQuery $.trim('string')
  • 超出預期的內容長度
  • 重複的內容 ex: Excel 欄位重複
  • (可勾選多個項目的狀況下) 指勾選了單一項目或者沒有勾選項目,就送出(submit)表格

使用者操作

  • 重複點選按鈕
  • 在輸入框按 Enter

操作順序

  • 使用者的操作順序,可能不按照功能設計的順序
  • 沒有選取任何項目,就提交(submit)或操作表格

錯誤訊息

執行PHP時顯示原始碼的錯誤

  • 確認伺服器是否能執行 PHP。驗證: phpinfo
  • 如果 PHP 使用 short tag語法撰寫,則 php.ini 檔案 需要開啟 short_open_tag = Off -> On

錯誤訊息: ERROR: 00000::

  • 原因: 想要刪除的資料不在 MySQL 資料庫內,進行刪除時,會發生的錯誤。
  • 解決方法: 先檢查該資料是否存在,再進行刪除。

錯誤訊息: phpinfo() has been disabled

PHP Warning:  phpinfo() has been disabled for security reasons in /path/to/phpinfo.php on line xx
  • 原因: 網管關閉使用 phpinfo

替代解決方法 for Linux   : 使用 linux command

  • /path/to/bin/php -i | grep "變數名稱"
  • /path/to/bin/php -m 列出載入的模組[2]

錯誤訊息: PHP syntax error “unexpected $end”

解決方法


unified coding style

variable: $var

  • PHP: starts with dollar symbol $var[3]
  • javascript: the first character can be a letter or _ or $, and the other characters can be letters or _ or $ or numbers.[4]

troubleshooting steps of function

simply the question

  • Enter the function or not really?
  • disable the logical judgement and just print text
  • check the if-else condition was satisfied or not cf: Java: String Comparison It's logical (compiler will not show the warning) but wrong.

references

  1. php - Create a folder if it doesn't already exist - Stack Overflow code snippet: mkdir('path/to/directory', 0755, true);
  2. PHP: extension_loaded - Manual
  3. PHP: Basics - Manual
  4. , and the other characters can be letters or _ or $ or numbers.