如何在 PHP 中使用 @ 符号

在 PHP 中,@ 符号用于控制错误。

换句话说,它是一个错误控制运算符。

其中我们将检查使用此符号作为错误控制运算符的两个选项。

在开始使用 @ 符号作为错误控制运算符之前,请考虑它不会使错误消失。
他们只是被隐藏起来。
此外,这可能会使调试更加困难,因为我们将无法看到代码有什么问题。

选项 2

让我们看看@符号的另一个使用示例:

<?php 
//Statement 1 
$result= $hello['123'] 
//Statement 2 
$result= @$hello['123'] 
?>

它只会运行语句 1,通知消息将显示如下:

PHP Notice: Undefined variable: hello.

选项1

一旦在表达式之前添加@ 符号,则该表达式可能创建的错误消息将被忽略。

如果 track_errors 功能被激活,则该表达式将创建一条错误消息并保存在 $php_errormsg 变量中。

该变量每次都会被覆盖。

让我们看一个例子:

<?php
//File error 
$file_name = @file('non_existent_file') or die("Failed in opening the file: error: '$errormsg'");
//It is applied for expression 
$value = @$cache[$key];
//It won’t show notice in case the index $key doesn't exist. 
?>

运行时错误如下所示:

PHP Notice:  Undefined variable: errormsg in
/home/fe14324b24d1add1aa38a0746a79bed.php on line 5

输出将是:

Failed in opening the file: error: ''
日期:2020-06-02 22:15:55 来源:oir作者:oir