Jump to content


This topic is now archived and is closed to further replies.


Whats with the @ symbol?

Recommended Posts

[b]Q:[/b] Whats does @ do?
[b]A:[/b] When you browse peoples code you may have came across a weird bit of PHP syntax which has an @ symbol infront of a function or variable like so:

$conn = @mysql_connect("localhost", "usr", "pass");

@mysql_select_db("foodb", $conn);


Ever wondered why and what it does? Well what the @ symbol does is supress errors, which means to force PHP to not show the error message. Why would you want to supress errors? You might not want any errors to be shown when your site goes live, such as if your database connection fails php will ouput an error message if error_reporting is turned

You shouldn't really use the @ symbol, actually you should [b]never[/b] have to use it if you trap errors correctly it should always be the last option if all other methods of trapping errors fail. Such as for when connecting to mysql in the above code you can do this instead:
[code]$conn = mysql_connect("localhost", "usr", "pass") or die("Error connecting to database");[/code]
What that now does is trigger the [i]or die[/i] cluase, which will stop your script from running and display a simple error message that you have defined yourself, if php was unable to connect to mysql, therefore it doesn't show this nastly looking error message:
[i]Access denied for user 'usr'@'localhost' (using password: YES)[/i]

You usually see the @ symbol used by what I call [i]lazy[/i] PHP programmers.

Hope that helps understand what the @ symbol does.

Share this post

Link to post
Share on other sites
[quote=Wildteen]$conn = mysql_connect("localhost", "usr", "pass") or die("Error connecting to database");[/quote]

When I run the above code I get -->

[quote]Warning: mysql_connect() [function.mysql-connect]: Access denied for user: 'usr@localhost' (Using password: YES) in mypage.php on line 2
Error connecting to database[/quote]

Whereas with the "@"

$conn = @mysql_connect("localhost", "usr", "pass") or die("Error connecting to database");

gives --> Error connecting to database

Share this post

Link to post
Share on other sites
From manual:

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.

Note: The @-operator works only on expressions...

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.