开发者

fopen with/without @ before it

开发者 https://www.devze.com 2022-12-25 01:43 出处:网络
I\'ve seen code samples th开发者_如何学Pythonat use an @ before fopen as in $fh = @fopen($myFile, \'w\');

I've seen code samples th开发者_如何学Pythonat use an @ before fopen as in

$fh = @fopen($myFile, 'w');

What's the significance of this @?


It suppresses errors an expression may display.

You can read more about it here.

Example:

file_get_contents('file_does_not_exist'); //this shows an error

@file_get_contents('file_does_not_exist'); //this does not


Its PHP's error control char.

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.

More here.


Using @ is always bad practice. It will suppress an error message if occurrs, while error messages are extremely useful for the programmer and suppressing it is suicide. PHP's fate is to be used mostly not by programmers but by casual users who have no clue. You've got this code from one of that latter kind. So, better to get rid of all @, so, you'll be able to see what happened and correct a mistake.
Note that every error message has it's particular meaning and explain what the problem is. For example there can be filesystem permissions problem or PHP OPEN_BASEDIR setting to prevent file from open. So, an error message will tell you what to do. Error messages is good and @ is evil.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号