Jump to content


Photo

Whats with the @ symbol?


  • Please log in to reply
2 replies to this topic

#1 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 June 2006 - 10:19 AM

Q: Whats does @ do?
A: 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:
<?php

$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 never 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:
$conn = mysql_connect("localhost", "usr", "pass") or die("Error connecting to database");
What that now does is trigger the or die 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:
Access denied for user 'usr'@'localhost' (using password: YES)

You usually see the @ symbol used by what I call lazy PHP programmers.

Hope that helps understand what the @ symbol does.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 July 2006 - 11:07 PM

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


When I run the above code I get -->

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


Whereas with the "@"

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

gives --> Error connecting to database


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 10 July 2006 - 03:46 AM

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...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users