加拿大华人论坛 美国华人新闻Introduction to PHP "@" Operator



在加拿大


PHP: The @ OperatorMay 7th, 2006I’ve seen some confusion online lately about the purpose of the “@” operator in PHP. Let’s figure out exactly what the “at operator” does.PHP holds your hand with error reporting during development by printing error messages to the user’s browser automatically when something goes wrong. This often looks something like this:Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host ‘woopsy.example.com’ (1) in errors.php on line 3The code that created this warning looks like this:mysql_connect( "woopsy.example.com" );Pretty simple — I tried to connect to a non-existent MySQL server using a mistaken hostname. This is handy, but I usually don’t like these kinds of errors to show up when a user browses my site. We can make the error go away by prepending a “@” on the front of the call to mysql_connect(), like this:@mysql_connect( "woopsy.example.com" );This gets rid of the warning message we saw earlier, but now we have no way of knowing whether mysql_connect() was actually able to connect or not. To fix that, we need to check the return value. According to the mysql_connect() documentation, the function returns a MySQL link identifier on success and FALSE on failure. So, let’s do this:$db = @mysql_connect( "woopsy.example.com" );if( $db === false ){ echo "Could not connect to server."; exit();}The lesson to take away from this is that the “@” operator suppresses error messages, not return values. You can read more about the operator at the PHP Error Control Operator documentation page.For the motivated reader, the “@” operatore has many more applications. You can use it to suppress errors in include files and even bad array indexes.The best way to deal with errors, however, is to use your own error handling function by calling set_error_handler() . This way, you can get notified whenever an error occurs, and you can even be told the error number, message, file, and line number. This way you can log it or even present a link for users to report the bug. It’s very handy, and a great way to find and fix bugs without requiring too much user inconvenience.The phpLDAPadmin project has an excellent implementation of an error handler, which can be viewed here. Search that page for pla_error_handler. It reports lots of versions (ie, the web server and PHP version), the exact error message, and even some PHP settings. It proved very useful for users to report bugs, since they could just copy/paste the output on the SourceForge bug tracker. ‘This leads me to my final comment. I’ve already mentioned that the best way to handle errors in PHP is to implement your own error handler and call set_error_handler(). In this function, you should log all errors, and then only display a message for those that happen without the “@” operator present. In other words, you should do something like this:function my_error_handler( $errno, $errstr, $file, $lineno ){ my_log_msg( "Error $errno: $errstr in file $file on line $lineno" ); if( 0 == ini_get( "error_reporting" ) || 0 == error_reporting() ) return; // Display an error message of some kind}Any now my final comment: I use error_reporting() in all my PHP applications to set the strictest error reporting possible like so:error_reporting( E_STRICT );You may not want such strict error reporting in your application, so choose from the available error levels. This will give you all kinds of error messages, but it will help you bullet-proof your code in a jiffy.Enjoy your error reporting!Souce : http://thesmithfam.org/blog/

  ·生活百科 一个朋友要生孩子了。姑娘,你想买什么礼物去拜访呢?
·生活百科 如何使用e卡?

美国华人新闻-加拿大

富坚义博开通推特

华人网摘要:经漫画家村田雄介和集英社确认,该账号为富坚本人。 图源:twitter 5月23日,推上出现了一个ID为富坚义博的新账号,简介上写着这是一个报告原稿进度的“官方()”并发布了一 ...

美国华人新闻-加拿大

日本议员提议立法禁止AV

华人网摘要:日本立宪民主党众议员堤かなめ在25日众议院内阁委员会上表示,立宪民主党将 "禁止涉及性行为的色情制品 "为目标制定法律。 日本立宪民主党众议员堤かなめ在25日众议院内阁 ...

美国华人新闻-加拿大

师村妙石的创新篆刻作品

华人网摘要:师村妙石,日本著名艺术家、篆刻家、书法家,一直致力于中日友好活动和文化交流,长期专攻中国传统篆刻技法。师村妙石先生曾223次访问中国进行书法研究和文化交流(截至 ...

日本,每年一次全民体检
美国华人新闻-加拿大

日本,每年一次全民体检

华人网关注我,带你看看普通人的日本生活。我家附近每隔两三百米就有一块告示板,类似于国内“居委会”或者“社区”的通知,平时大事小情都能在这里看到。前几天看到了有免费筛查宫 ...

美国华人新闻-加拿大

师村妙石的编著书影

华人网摘要:师村妙石,日本著名艺术家、篆刻家、书法家,一直致力于中日友好活动和文化交流,长期专攻中国传统篆刻技法。师村妙石先生曾223次访问中国进行书法研究和文化交流(截至 ...