What does @ character do in PHP?

Possible duplicates:
Link - what does this symbol mean in PHP?
What does @ mean in PHP?

I have a line in my code that looks like this:

@mysql_select_db ($ dbname) or die ("Error: Unable to select database");

It works, but I want to know what @ does and why it is there.

+4
source share
3 answers

In this case, @ will suppress the usual PHP database connection error (which may contain sensitive information). In case of a connection error, the "or die" part will be executed, otherwise with a general error message. The string is probably copied from a "quick and dirty" example.

Using the error suppression operator @ is considered bad style, especially when there are no other forms of error handling. This complicates the debugging - how can you find out about the error without any indication that this happened? In a production system, it is better to write all errors to a file and suppress the display of errors on the page. You can do this in the php.ini or (if you are on a shared host and cannot change the configuration) with the following code.

 ini_set('display_errors', false); ini_set('log_errors', true); ini_set('error_log', '/var/log/apache/php-errors.log'); 
+2
source

The @ character suppresses any errors or notifications for the expression preceding it.

See this link: PHP Error Management Operators

PHP supports one error control statement: the at (@) sign. when added to an expression in PHP, any error messages that may be generated by this expression will be ignored.

+13
source

It suppresses all errors. Generally, you should not use it unless you have a good reason. I do not know why it is used in the example you posted, or why die () is used. The error must be caught and handled accordingly. The choice may fail for a number of reasons, some of which may be restored. Like no connection to an established database.

+1
source

Source: https://habr.com/ru/post/1332337/


All Articles