2012-03-23 52 views
0

我從一個網站上看到一段代碼,它在php中。在那個代碼變量前寫@符號是什麼意思 例如:PHP @ variables

@hello =「hi」;

+3

我只能想象這是一個錯字;你能提供一個到相關網頁的鏈接嗎? – sg3s 2012-03-23 08:20:41

+1

這實際上是一個解析錯誤,它不是有效的PHP代碼。 – deceze 2012-03-23 08:23:14

+0

它是Ruby,而不是PHP。 – Gordon 2012-03-23 08:39:05

回答

3

@是PHP中的錯誤抑制運算符。

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. 

編輯:

典型用途包括以下內容:

<?php 
// Usage of the @ symbol in PHP code 

// Typical Example 
$var = @some_function(); 

// Class/Object Example 
$var = @new some_class(); 

// Does NOT Work! 
//$var = new @some_class(); // syntax error 

// Another example. Very slow 
$var = @$some_var; 

?> 

參見下面的鏈接:

http://php.net/manual/en/language.operators.errorcontrol.php

http://michelf.com/weblog/2005/bad-uses-of-the-at-operator/

+1

而語法是:'$ someVar = @SomeFunction();'意味着每個錯誤或異常拋出SomeFunction它被丟棄。 – TJHeuvel 2012-03-23 08:23:46

+0

這很好,所有的,但發佈的示例代碼將不會執行由於解析錯誤。 – deceze 2012-03-23 08:25:49

+0

這就是爲什麼我在上面的答案中也包含了'at運算符用法不好'的鏈接。 – Frankline 2012-03-23 08:26:55

0

@不能與沒有$的變量一起使用。這是語法錯誤。

+0

那麼'echo @foo;'實際上會工作並輸出「foo」。只是分配不起作用。 – deceze 2012-03-23 08:26:39

+0

在這種情況下,foo是未定義常量。不是一個變量。它輸出爲字符串和PHP通知。但@壓制它。看起來像只寫。不要在家裏這樣做=) – 2012-03-23 08:59:45