PHP alias @ function
Asked Answered
U

7

5

I'm new to PHP and I'm confused seeing some examples calling a function with a @ prefix like @mysql_ping().

What is it for? Googling / searching is not much of a help since @ gets discarded and 'alias' is not good enough keyword.

Unrelenting answered 26/9, 2008 at 7:33 Comment(0)
S
15

@ suppresses errors, warnings and notices.

You can use it for good purpose if you complement it with a custom error handler or with due check of $php_errormsg variable so you can handle errors properly.

In my experience, this proper usage is not seen very much and is instead used a lot in the bad way, just to hide errors without acting on them.

More info at http://www.php.net/manual/en/language.operators.errorcontrol.php

Scoliosis answered 26/9, 2008 at 7:36 Comment(5)
Well lazy... Let's say it's usefull for a "run once, throw away" script. In any overcase, it's a bad practice.Baggett
It's terribly useful to suppress error notices if you otherwise have an error handler in place.Hambletonian
"It's usually used by lazy programmers who don't want to check error codes" - Completely wrong. @ is exactly how PHP does its "try / catch" php.net/manual/en/language.operators.errorcontrol.phpTorque
Very, very removed from a real try/catch though. Standard "if you know what you are doing and have error handlers in place then it's okay" disclaimer. sighScoliosis
@Torque its not how php does try catch. Php uses ... wait for it ... try / catch (php.net/manual/en/language.exceptions.php). The @ error suppression is just that, error suppression. It can be useful in some circumstances but should be used with care.Sarracenia
D
3

It suppresses the output of error messages. Contrary to another commentator here, I think that it is good programming practice to use it (especially if you are developing a web app, where the output would be mixed in the html of the output page).

Functions like mysql_connect return a resource identifier, or FALSE on errors. Use @mysql_connect(...) and check the return value.

Darrendarrey answered 26/9, 2008 at 7:37 Comment(0)
I
2

Googling for "php at symbol" suggests that it asks PHP to not display any error messages that the call causes.

Illuminism answered 26/9, 2008 at 7:36 Comment(0)
R
1

It suppresses any errors that might otherwise be output.

It is a recipe for pain and hardship, as it inevitably leads to difficulties when an error does occur, you are bound to spend hours tracking down the cause. If the @ operator hadn't been used, then the error would have been found in seconds.

There is no good reason to use it, use the display_errors and error_log ini settings to prevent errors from displaying on a live site, and let them be shown on your dev site.

If there is an error that you don't want to see, you're better off just fixing it than suppressing it!

If it's something in an external lib and outside your control, just write it to the logs, turn off display_errors on production, and live with it. Because there's no telling whether the error you're suppressing now and are happy to live with will ALWAYS be the error that is thrown from there.

@ === BAD

Ripuarian answered 26/9, 2008 at 7:42 Comment(0)
C
0

Suppress error messages: http://bytes.com/forum/thread10951.html

Corrugate answered 26/9, 2008 at 7:37 Comment(0)
R
0

Prefixing a function with the a symbol stops it triggering the PHP error handler if an error occurs. Bear in mind that you must do all the error handling yourself if you decide to use it.

$test = @file_get_contents('nonexistant.file');
if(!$test)
{
    die('Failed');
}

A better practice is to turn display_errors off and use custom error handlers (see Error Exception).

Russi answered 26/9, 2008 at 7:42 Comment(0)
H
0

Sometimes it is useful- especially if the admin doesn't want you to play with the php environment or the value isn't important and is mainly cosmetic. Do remember, though; it's a workaround, not a panacea.

[...]

.$foutDate = @filemtime($keyring); /* Don't care, as we've already established file */

$f["date"] = $foutDate;

$f["fullDate"] = date("r", $foutDate);

[...]

Hoarding answered 6/5, 2010 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.