Error Control Operators @

本文详细介绍了PHP中用于忽略表达式错误的@操作符的使用方法、注意事项及应用场景,包括如何设置自定义错误处理器、保存错误消息到变量、避免关键错误导致脚本终止等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Error Control Operators

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.

<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die (
"Failed opening file: error was '$php_errormsg'");

// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.

?>

Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.

See also error_reporting() and the manual section for Error Handling and Logging functions.

Warning

Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.


一、关键字(Keywords) 关键字是C语言预定义的保留标识符,具有特定功能,不可作为变量名使用。例如: 数据类型: i n t int、 f l o a t float、 c h a r char、 d o u b l e double 控制结构: i f if、 e l s e else、 f o r for、 w h i l e while、 s w i t c h switch、 c a s e case 函数相关: r e t u r n return、 v o i d void、 s i z e o f sizeof C int main() { // int和return均为关键字 return 0; } 二、标识符(Identifiers) 标识符用于命名变量、函数等实体,规则包括: 由字母/下划线开头,可包含数字 区分大小写( A g e Age与 a g e age不同) 不可与关键字冲突 C int age; // 合法标识符 float _value; // 合法标识符 char 3year; // 非法(数字开头) 三、常量(Constants) 1. 整型常量 十进制: 123 123 八进制: 0173 0173(以0开头) 十六进制: 0 x 7 B 0x7B 2. 浮点常量 标准形式: 3.14 3.14 科学计数法: 1.23 e − 5 1.23e−5 3. 字符常量 用单引号包裹: ′ A ′ ′ A ′ 、ParseError: KaTeX parse error: Undefined control sequence: \n at position 2: '\̲n̲'(转义字符) 4. 枚举常量 C enum Color { RED, GREEN }; // RED=0, GREEN=1 5. 宏定义常量 C #define PI 3.14159 // 预处理阶段替换 四、字符串字面量(String Literals) 双引号包裹的字符序列,以ParseError: KaTeX parse error: Undefined control sequence: \0 at position 2: '\̲0̲'结尾: C char str[] = "hello"; // 存储为['h','e','l','l','o','\0'] 五、运算符(Operators)3 类型 示例 算术运算符 + +、 − −、 ∗ ∗、 / / 赋值运算符 = =、 + = +=、 ∗ = ∗= 关系运算符 = = ==、 > >、 ! = != 逻辑运算符 ParseError: KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲&、$ 三目运算符 ( a > b ) ? a : b (a>b)?a:b 六、分隔符(Separators)1 大括号 :定义代码块 分号 ; ;:语句结束符 逗号 , ,:变量分隔 冒号 : ::三目运算符/标签 C if(a>b) { // {}为代码块分隔符 printf("%d",a);以下是一份针对C语言基础语法中 **关键字、标识符、常量、字符串字面量、运算符、分隔符** 的详细说明笔记: --- ## **1. 关键字(Keywords)** - **定义**:C语言预定义的保留字,具有特殊含义,不可用作标识符。 - **分类**(C89标准共32个关键字): ```c auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while ``` - **注意事项**: - 所有关键字均为小写(如 `int` 正确,`INT` 不是关键字)。 - 例如:`int num;` 中的 `int` 用于声明整型变量。 --- ## **2. 标识符(Identifiers)** - **定义**:由程序员自定义的名称,用于变量、函数、结构体等。 - **命名规则**: 1. 由字母、数字、下划线组成,且**首字符不
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值