Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen with/without @ before it

Tags:

file

php

file-io

I've seen code samples that use an @ before fopen as in

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

What's the significance of this @?

like image 883
brett Avatar asked Mar 28 '10 02:03

brett


People also ask

What is X+ mode used for in fopen ()?

"x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists.

What is FTP Fopen?

fopen() binds a named resource, specified by filename , to a stream.

When using the fopen () function to open a file in PHP What made should you use for appending data to a file?

One can append some specific data into a specific file just by using a+ or a mode in the fopen() function. The PHP Append to file is done just by using the fwrite() function of PHP Programming Language.

How many types of modes are being there in the fopen function elaborate all of them in PHP?

The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.


3 Answers

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
like image 90
Tim Cooper Avatar answered Oct 01 '22 16:10

Tim Cooper


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.

like image 24
codaddict Avatar answered Oct 01 '22 16:10

codaddict


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.

like image 43
Your Common Sense Avatar answered Oct 01 '22 16:10

Your Common Sense