抑制php错误?…为什么?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了抑制php错误?…为什么?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
嘿伙计们,我想知道为什么你会“抑制”一个PHP错误?我明显看到了从错误中吐出的额外线的差异……但抑制它有好处吗?
Access denied for user 'user'@'localhost' (using password: YES)

VS

Warning: MysqL_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost'       (using password: YES) in (deleted) on line 8
Access denied for user 'user'@'localhost' (using password: YES)

如果是这样,我应该养成在我的PHP中的mySQL查询开头输入@的习惯吗?

谢谢!

你应该积极地养成抑制错误的习惯.错误是有原因的.相反,在代码中正确和御地处理它们,并不断完善代码,直到错误消失.

你应该做的事情如下:

$conn = MysqL_connect($host,$user,$pass);

// Always test to see if your action/connection/whatever was successful
if (!$conn) {
  // something went wrong.  handle the error
  // Display a message for the user,wrITe a message to `error_LOG()`,whatever's apPRopriate
}
else MysqL_select_db($dbname);

在生产系统上,您永远不应该显示错误,因为它可能会泄露您的代码数据库的详细信息.相反,在PHP.ini或运行时关闭display_errors:

// In development and production,make sure all errors are reported
error_reporting(E_ALL & E_STRICT);

// In development show all errors on screen so you handle them as they occur
ini_set('display_errors',1);

// In production turn them off
ini_set('display_errors',0);

实际上,使用@的错误抑制是PHP糟糕练习in this classic question.的第二大投票

脚本宝典总结

以上是脚本宝典为你收集整理的抑制php错误?…为什么?全部内容,希望文章能够帮你解决抑制php错误?…为什么?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。