Valid use cases for at-sign in php

前端 未结 6 1393
天命终不由人
天命终不由人 2021-01-23 09:16

The at sign is used to hide error messages. As far as I see, there is absolutely no use case or excuse for using it.

  1. You can hide errors in production by changing
6条回答
  •  天涯浪人
    2021-01-23 09:32

    There are a few rare situations where it indeed makes sense to use error suppression.

    One of them are atomic filesystem operations. E.g. instead of writing

    if (file_exists($fileName)) {
        unlink($fileName);
    }
    

    you just do

    @unlink($fileName);
    

    This makes sure that your code is not subject to race conditions.

    Generally @ is useful in situations where PHP chose an inappropriate error model for a function. The above unlink function is one such example. Similarly there are other functions where PHP throws errors, even though it shouldn't (instead using return values or catchable exceptions).

提交回复
热议问题