Results 1 to 25 of 26
Thread: php question...
-
04-04-2009, 02:41 PM #1Disabled
- Join Date
- Mar 2009
- Posts
- 155
php question...
what does @ in front of a function mean?
Example:
@ini_get()
-
04-04-2009, 02:47 PM #2Junior Guru Wannabe
- Join Date
- Jul 2008
- Posts
- 45
It supresses the error/warning that the function may display to the browser
-
04-04-2009, 06:04 PM #3Temporarily Suspended
- Join Date
- Apr 2009
- Posts
- 21
Like the person above me said, it supresses the errors/warnings generated from showing in the browser for the function you put it in front of.
For example:
$sql = @mysql_query("SELECT id from tabletest WHERE user='$user' limit 1");
-
04-04-2009, 06:06 PM #4Junior Guru Wannabe
- Join Date
- Oct 2008
- Posts
- 85
Handy little function when you get some annoying warning/error messages, of course this is no substitute for bad coding.
Anyone like to play games? check out http://www.buttonbashers.co.uk
██http://www.DataHostDirect.com Celebrating 6 months in business! 25% off for life! Shared, VPS and Dedicated Servers(Managed) ██
██20 minute average wait time on Support Tickets. With 24/7/365 Support██
-
04-04-2009, 07:33 PM #5WHT Addict
- Join Date
- Mar 2007
- Location
- UK
- Posts
- 157
More accurately - it suppresses PHP errors for expression.
@ = error-control operator prefix
So if your PHP server is configured to display messages in the browser, with @ it wont. And if you have PHP set to create error logs, any expression with @ in front of it wont create an entry in the error.log file either.
Be careful with it though as it can hinder fault finding.
It will disable error reporting for even critical errors that will terminate script execution, so typos and other often easy to find problems with errors wont be reported to screen or log file and are then hard to debug.
Typically you should use error reporting to a log file anyway IMO. You don't want errors showing to the screen on a live/public site for visitors to see, ugly and potential security risk.
So if avoiding using @ in development to allow for debugging AND displaying errors to the browser means you then have to go through all your code and add @ to everything you want, which isn't great. Which is why I use error.logs.
You can actually email errors if you so desired. Although as DHD-Chis said, "this is no substitute for bad coding" and I'd be worried if I had to rely on my sites emailing me whenever there was an error.
Have a read of this:
http://www.php.net/manual/en/languag...rorcontrol.php
And this
http://www.php.net/manual/en/ref.errorfunc.phpLast edited by greg2007; 04-04-2009 at 07:39 PM.
-
04-05-2009, 03:00 AM #6WHT Addict
- Join Date
- Jul 2008
- Posts
- 143
I've honestly never seen a legitimate use for it. It is also an extremely slow operation, as it really does temporarily turn error reporting off and then back on again.
-
04-05-2009, 05:44 AM #7Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
I can think of 2.
1) Your web host has errors on and you don't want any showing.
2) Some function will generate an error even if you check for it i.e. mkdir(),fopen()
-
04-05-2009, 06:37 AM #8Aspiring Evangelist
- Join Date
- Dec 2007
- Location
- Lebanon
- Posts
- 413
3) if you're trying to crawl some sites for whatever reason, connection errors might happen, and some errors could happen that you can never control
nothing is done without any need
-
04-05-2009, 07:09 AM #9WHT Addict
- Join Date
- Jul 2008
- Posts
- 143
Can be changed at runtime.
2) Some function will generate an error even if you check for it i.e. mkdir(),fopen()
3) if you're trying to crawl some sites for whatever reason, connection errors might happen, and some errors could happen that you can never control
-
04-05-2009, 07:34 AM #10Aspiring Evangelist
- Join Date
- Dec 2007
- Location
- Lebanon
- Posts
- 413
even if the code is properly trapped, some errors happen before u seeing them... so u use the @ to ensure it
-
04-05-2009, 10:36 AM #11WHT Addict
- Join Date
- Mar 2007
- Location
- UK
- Posts
- 157
Again as DHD-Chris said, there is no substitute for bad coding.
You should be avoiding any errors from being output, such as checking if something is an array BEFORE running a foreach on it.
That said, there are circumstances out of the control of the coder.
You can have something like
Code:$db_connection = @mysql_connect($db_host, $db_user, $db_password);
Others are mysql_query, mysql_num_rows etc. If for some reason a table name gets altered or isn't accessible then running the above code will result in an error, something like: mysql_num_rows is an invalid result resource blah..
It isn't always about bad coding or even being a good coder and having code that avoids errors. There are outside influences beyond you control, and therefore using the @ in places where problems can happen makes you a good coder!
I'm not saying use it everywhere. Like Veks said, you should be using things like is_dir, is_array, if !empty etcLast edited by greg2007; 04-05-2009 at 10:41 AM.
-
04-05-2009, 12:47 PM #12Web Hosting Master
- Join Date
- Nov 2001
- Location
- Vancouver
- Posts
- 2,422
Seems to me that intelligent use of exception handling and doing something with them might be a better approach. Some - like legitimately returning no rows or some such thing - are conditions that one expects to see and thus the application should continue.
But others - like a database password change which causes the entire site to break - should be allowed to break the site (and trigger a notification to the sysadmin) or caught and intelligently handled (like presenting an "off line" page and notifying the sysadmin).
On a development machine, you want to display *uncaught* to the developer/QA person.
On a public machine, un-caught exceptions (or intelligently caught exceptions) get send as an email to your site admin/developer. An uncaught exception is an "exceptional circumstance", one they should be alerted to, instead of pretending all is well. In Python we might do something like:
Code:try: user = get_user(email_address) except DatabaseException as e: notify("get_user failed retrieving email_address", e) return system_offline() # all is well, carry on do_foo_with_user(user)
Code:user = get_user(email_address) do_foo_with_user(user)
“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”
-
04-05-2009, 02:25 PM #13WHT Addict
- Join Date
- Mar 2007
- Location
- UK
- Posts
- 157
You have to draw a line somewhere otherwise it would take forever to complete code.
I'm not suggesting cutting corners, some things are a must, like security (real_escape etc).
But with having something like an exception for a database connection, it's more coding time and more resource usage for the server.
In most practical environments you would expect that mysql_connect is going to find and successfully connect to a database, and mysql_query is debugged in development, the table exists, the queried fileds exist and contain data as required (of course still with a clause where if rows <=0 etc.)
So, using an @ on mysql_connect is simply a backup that more than likely will never be used. It's not supposed to be a useful alternative to informing admin, site owner or site visitors there was an issue please try again etc, it's simply to stop PHP errors being reported to site visitors.
<If can connect then do stuff else email admin blah etc> is a waste of resources and time to code in 99% of cases as usually DB connections will work.
If you are having db connection problems on a regular basis then you need to look elsewhere for solutions.
And mostly scripts don't rely on a connection for something that would be detrimental without a connection anyway, as you would likely have something in place within the script to avoid that.
Such as "Sorry my product could be displayed" "sorry we could log you in please try again" etc
I have built many websites, and manage many still, and don't think in the past few years a database wasn't connectible.
Apart from if the entire site is down, but then the code wont be accessed and executed anyway.
-
04-05-2009, 02:52 PM #14Web Hosting Master
- Join Date
- Nov 2001
- Location
- Vancouver
- Posts
- 2,422
That's funny, I've seen a great many PHP sites respond with HTTP 500 errors, commonly variations of:
"Fatal error: Out of memory"
"MySQL: Too Many Connections"
"Database Error: Unable to connect to the database:Could not connect to MySQL" (joomla sites)
and so on. Once upon a time you'd see such page results show up in web searches by the hundreds of thousands but for some time now search providers have filtered out 500 errors it seems.
<If can connect then do stuff else email admin blah etc> is a waste of resources and time to code
Better yet if your web framework is designed to trap uncaught exceptions, then you can allow the framework to catch these and notify appropriately.
I grab my database object and work with it. I don't need to deal with thinking about these exceptions because if the DB should die and a transaction is not completed, it won't be committed. And I'll get a note or a page about the situation. Its no extra work at all to get this; write the routine / framework once, use many, many, times thereafter.
in 99% of cases as usually DB connections will work. If you are having db connection problems on a regular basis then you need to look elsewhere for solutions.
In PHP "@", wantonly applied, is as dangerous and foolhardy as the following, applied without discrimination, in Python:
Code:try: do_something_that_might_fail() except: pass # do nothing
To each his own, I suppose. I like to sleep at night, knowing that my apps will keep me informed of bad juju happening.“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”
-
04-05-2009, 03:49 PM #15Aspiring Evangelist
- Join Date
- Dec 2007
- Location
- Lebanon
- Posts
- 413
for me nothing goes unseen... I never put the @ before testing... I write the code I test the site I see the errors that could happen and put comments, then I use the @ so that I can continue working (before using time on error handling) and when time is good (I'm on schedule and have nothing to do, or I am faster than the schedule) I handle the errors... I am not talking about simple code, I am talking about complex code which needs to be done on time
-
04-05-2009, 03:51 PM #16WHT Addict
- Join Date
- Mar 2007
- Location
- UK
- Posts
- 157
-
04-05-2009, 10:19 PM #17Web Hosting Master
- Join Date
- Nov 2001
- Location
- Vancouver
- Posts
- 2,422
I subscribe to the philosophy that all code is $hit. Mine too. Vainglorious conceit as to the quality of one's code will not hide the true smell.
I sleep well knowing that my mistakes will be picked up.“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”
-
04-06-2009, 12:07 PM #18WHT Addict
- Join Date
- Mar 2007
- Location
- UK
- Posts
- 157
Hmm, truth be known, I don't need mine to tell me when it doesn't work, I don't usually get any sleep
And don't try to impress me with your big words! I have a GCSE in Home Ec
-
04-06-2009, 01:02 PM #19WHT Addict
- Join Date
- Jul 2008
- Posts
- 143
-
04-06-2009, 02:18 PM #20Web Hosting Master
- Join Date
- Nov 2001
- Location
- Vancouver
- Posts
- 2,422
-
04-06-2009, 03:58 PM #21Aspiring Evangelist
- Join Date
- Dec 2007
- Location
- Lebanon
- Posts
- 413
what if you're reading an xml file from another site, and it has errors? how would you solve this problem? You contact the site that owns the xml file telling him to fix his file? or you stop reading all other files to fix what's happening with this file (if you can fix it)?
-
04-06-2009, 05:09 PM #22Aspiring Evangelist
- Join Date
- Dec 2007
- Location
- Lebanon
- Posts
- 413
it's a known project management way called: Do it twice - Quickly and correctly.
First time: do it quickly (for sure working) which will help you in gaining market share and money to be able to survive
Second time: do it correctly which will be the end product that will remain and you'll be able to build over
I am not saying that's the perfect neither that it's not risky, BUT no risk no gain I accept the riskLast edited by Codebird; 04-06-2009 at 05:12 PM.
-
04-06-2009, 10:24 PM #23WHT Addict
- Join Date
- Jul 2008
- Posts
- 143
It depends on the situation. I may be parsing it with a regular expression, in which case I have caught and handled all errors appropriately.
If I'm using the XMLReader class, I make use of php's exception handling. The same would go for mysqli in php 5.3.
it's a known project management way called: Do it twice - Quickly and correctly.
First time: do it quickly (for sure working) which will help you in gaining market share and money to be able to survive
Second time: do it correctly which will be the end product that will remain and you'll be able to build over
I am not saying that's the perfect neither that it's not risky, BUT no risk no gain I accept the risk
-
04-06-2009, 10:29 PM #24Web Hosting Master
- Join Date
- Mar 2009
- Posts
- 2,222
-
04-06-2009, 11:17 PM #25WHT Addict
- Join Date
- Jul 2008
- Posts
- 143
Eh, sometimes it can be, like if you go sitemap trolling and really want to map a number of sites.
"Why are you doing that in php!?" <- I don't. I do it in python, but this is a php thread : )