Page 1 of 1
@ at the beginning of a line
Posted: Tue Dec 14, 2010 8:16 am
by whkowiak
hey there,
ok the question is certainly basic but I couldn“t find an answer on google
I saw on a tutorial the use of @ at the beginning of the line, before a variable, having the effect of "cancel" it (or as far as I understood to have it not interpreted)
is it what @ is to be used for in php?
Re: @ at the beginning of a line
Posted: Tue Dec 14, 2010 8:27 am
by social_experiment
@ is a error control operator in php.
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 2:47 am
by jagannathg
social_experiment wrote:@ is a error control operator in php.
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
Hello social_experiment,
Is it a best practise to use @ for error handling?
Regards,
Jagannath
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 3:17 am
by social_experiment
It's a pretty good practise IMO. Let's say you are connecting to a SQL database server and for some or other reason the connection cannot be made (wrong login details, server down, etc). If you don't suppress the error originating from mysql_connect() the resulting error message displays information that might be useful to a malicious user.
If you do use it, use it only if your script is live, while you develop and test don't suppress the errors because they help you find and diagnose the issues you might be having.
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 3:48 am
by Darhazer
First, error suppression operator is not error handling - it is an error suppression
It is very bad practice. What social_experiment says is correct - you should not display the original error messages in production, but the way to solve this is just to disable them in the settings.
Never use error suppression - handle the errors.
Also, suppressing error messages makes finding bugs in the code much more difficult. If you use @ before mysql_connect and mysql extension is not enabled, you'll have a fatal error, but the error is suppressed so you only see a blank page.
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 8:03 am
by superdezign
Darhazer wrote:It is very bad practice.
QFT.
Error suppression is a lazy method of "handling" errors. It will shorten the amount of code that you write, but in my experience it will also slow down PHP's processing, especially if you are using a function that you didn't check to be sure that you could use beforehand. Generally, you can do error checking
before using any operations that would toss errors. Things like
empty() and
is_resource() are your friend.
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 8:17 am
by social_experiment
Yeah, error suppression isn't handling it. Common sense dictates that you don't leave a situation where an error might occur without a notification (or explanation) of the problem.
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 9:29 am
by s.dot
Very rarely should you use the @ operator
It is lazy and ugly and leaves so much room for errors
Re: @ at the beginning of a line
Posted: Wed Dec 15, 2010 9:57 am
by Apollo
Well, under NO CIRCUMSTANCES would I want to expose PHP errors to random users. But obviously I do want to see them myself (and fix them)
What I do in some situations is suppress all warnings & error message with error_reporting(0)
unless the visitor is logged in as administrator (i.e. me or someone else who should see errors), or if some secret cookie is set (e.g. if the site doesn't use any login mechanism).
Obviously you should still properly handle errors to the extent of informing users something went wrong. As in:
Code: Select all
if (!send_mail(...)) print('Sorry, your mail could not be sent!');
Re: @ at the beginning of a line
Posted: Sat Dec 18, 2010 11:08 am
by whkowiak
thank you all for the answers and the explanations.
it looks to me that it is the same thing with error notifications and notices
I read many people considering that modifying the php.ini to have php not display errors or notices is the way to solve the problem, which obviously is nothing but hiding it (it is still there and the code is stil not perfect)