@ at the beginning of a line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

@ at the beginning of a line

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: @ at the beginning of a line

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jagannathg
Forum Commoner
Posts: 34
Joined: Wed Dec 08, 2010 1:55 am

Re: @ at the beginning of a line

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: @ at the beginning of a line

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: @ at the beginning of a line

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: @ at the beginning of a line

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: @ at the beginning of a line

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: @ at the beginning of a line

Post by s.dot »

Very rarely should you use the @ operator
It is lazy and ugly and leaves so much room for errors
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: @ at the beginning of a line

Post 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!');
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

Re: @ at the beginning of a line

Post 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)
Post Reply