PHP 中 @ 符號有什麼用途?


PHP 支援錯誤控制運算子,即 @ 符號。當在表示式的前面加上 @ 時,該表示式可能產生的任何錯誤訊息都會被忽略。

若要在 PHP 中使用 @ 符號,程式碼如下−

示例

 Live Demo

<?php
   $file = @file ('non_existent_file') or
   die ("Failed in opening the file: Error Message = '$err'");
   $value = @$cache[$key];
?>

輸出

這將產生以下輸出−

Failed in opening the file: Error Message = ''PHP Notice: Undefined
variable: err in /home/cg/root/6985034/main.php on line 4

示例

現在我們來看另一個示例−

 Live Demo

<?php
   $val = $test['5']
   $val = @$test['5']
?>

輸出

這將產生以下輸出−

PHP Parse error: syntax error, unexpected '$val' (T_VARIABLE) in /home/cg/root/6985034/main.php on line 5

更新於: 2019 年 12 月 26 日

1000+ 閱讀

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.