Home > Backend Development > PHP Problem > Have you ever used the @ operator in php?

Have you ever used the @ operator in php?

醉折花枝作酒筹
Release: 2023-03-11 22:36:02
Original
2216 people have browsed it

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(&#39;grand.php&#39;) or die(&#39;文件格式发生错误!&#39;);
$value = @ $cache[$key];
?>
Copy after login

What is the result of this small example? You can take a guess. Okay, let's announce the results now.

Have you ever used the @ operator in php?

#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 (&#39;不存在的文件名称&#39;) or
    die ("打开文件失败:错误为&#39;" . error_get_last()[&#39;message&#39;] . "&#39;");
    //适用于任何表达式,而不仅仅是函数:
    
$value = @$cache[$key];
//如果索引$key不存在,则不会发出通知。
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template