Home > Article > Backend Development > Have you ever used the @ operator in php?
Today we introduce an operator, which may be slightly different from the previous operators, but this operator is very useful and you can refer to it if necessary.
We are introducing the error control operator. Before introducing the usage of this operator, let’s take a look at this operator.
Error control operator, computer language, PHP supports an operator @
. When placed before a php expression, any error message that expression may produce is ignored.
If the track_errors
feature is activated, any error messages generated by the expression are stored in the variable $php_errormsg
. This variable is overwritten on every error, so check it as early as possible if you want to use it.
We already know what the error control operator is, so let’s take a look at how to use this operator. Let’s look at a small example first.
<?php $my_file = @file('grand.php') or die('文件格式发生错误!'); $value = @ $cache[$key]; ?>
What is the result of this small example? You can take a guess. Okay, let's announce the results now.
#There should be many people who have guessed it right. That’s great, give yourself a round of applause. The output result of this example seems to be the content after die. So what does this operator do? How to use it?
Let’s take a look at the usage of this operation operator.
<?php $my_file = @file ('不存在的文件名称') or die ("打开文件失败:错误为'" . error_get_last()['message'] . "'"); //适用于任何表达式,而不仅仅是函数: $value = @$cache[$key]; //如果索引$key不存在,则不会发出通知。 ?>
This is the usage of this operator, but let’s look at some precautions or other things.
For newbies, a simple rule is: if you can get a value from somewhere, you can add the @ operator to it. For example, you can place it before variables, functions and include
calls, constants, etc. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if
and foreach
.
At the same time, we must be aware that:
The current "@" error control operator prefix will even invalidate error reporting of serious errors that cause the script to terminate. This means that if you suppress an error message using "@" before a function call that does not exist or has the wrong letter typed, the script will show no indication of the cause.
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of Have you ever used the @ operator in php?. For more information, please follow other related articles on the PHP Chinese website!