Home >Backend Development >PHP Tutorial >What is the use of @ symbol in PHP

What is the use of @ symbol in PHP

藏色散人
藏色散人Original
2019-01-22 16:02:2036401browse

The at symbol (@) is used as an error control operator in PHP. When an expression is appended with the @ symbol, error messages that may be generated by the expression are ignored. If the track_errors feature is enabled, the error message generated by the expression will be saved in the variable $php_errormsg. This variable is overwritten with every error.

What is the use of @ symbol in PHP

Recommended manual: php complete self-study manual

Example 1

<?php 
  
// 文件错误 
$file_name = @file (&#39;non_existent_file&#39;) or
    die ("Failed in opening the file: error: &#39;$errormsg&#39;"); 
  
// 它用于表达
$value = @$cache[$key]; 
  
//如果索引$key不存在,它将不显示通知。 
  
?>

Runtime Error:

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

Output:

Failed in opening the file: error: &#39;&#39;

Example 2

<?php 
  
// 语句1
$result= $hello[&#39;123&#39;] 
  
// 语句2
$result= @$hello[&#39;123&#39;] 
?>

It will only execute Statement 1 and displays the notification message

PHP Notice:  Undefined variable: hello.

NOTE: Using @ is a very bad programming practice because it doesn't make the errors go away, it just hides them, and it makes debugging harder It sucks because we can't see what's actually wrong with our code.

Related article recommendations:
1.Exception and error analysis in php
2.php error control operator @ or die () Detailed explanation of usage examples
Related video recommendations:
1. Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of What is the use of @ symbol in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to annotate in PHPNext article:How to annotate in PHP