1

I want to know where and how to use the at sign, like the example below.

if (!@$zip)
  exit();
  • What is it, is there a reference to it?
  • How can i use it?
CSD
  • 169
  • 5
  • 1
    It's an [error control operator](https://www.php.net/manual/en/language.operators.errorcontrol.php) that suppresses error messages. You should avoid using it though since it will make debugging a real pain. – M. Eriksson Jul 22 '19 at 18:42
  • +Magnus Eriksson It will ignore the generated error if zip doesn't exist? – CSD Jul 22 '19 at 18:45
  • I posted a link in my first comment. It's to the manual about it. – M. Eriksson Jul 22 '19 at 18:47
  • It allows you to disable errors and warnings from a function or command which you might know to produce one. If used im not a 100% how critical errors will be handled though. If you ask me, then best practice is to avoid using it and try validating your way out of any notices, warnings or errors. If you use it, it can complicate your ability to debug your code since some potentially warnings or errors wont be shown. – SeeQue Jul 22 '19 at 18:51
  • 1
    @SeeQue - It will suppress all errors/warnings/notices, except from catchable errors (which you handle with `try/catch` instead). So if an expression causes a critical error that can't be caught, then it will silently just stop executing the script. – M. Eriksson Jul 22 '19 at 18:54
  • 1
    Ok, thanks for all the comments – CSD Jul 22 '19 at 18:55
  • @MagnusEriksson so a critical error will just fail silently ? – SeeQue Jul 22 '19 at 18:56
  • 1
    It means "I saw this somewhere and no one warned me not to use it". – ceejayoz Jul 22 '19 at 18:57
  • 1
    @SeeQue - Yes, that's why it's a very bad way of handling errors. However, since PHP 7, many of the critical errors are now thrown as catchable errors instead, which you can't silence using `@`. – M. Eriksson Jul 22 '19 at 18:57

1 Answers1

2

In PHP, the @ is a control operator. When prepended to an expression, any error messages generated by it (except catchable errors) will be ignored.

JohnL
  • 105
  • 1
  • 3
  • 12