/    Sign up×
Community /Pin to ProfileBookmark

"@" symbol on "include" directive

Hello,

I am using an “autoload” function to load the necessary files. Since it does some trying around it is very likely that some of the “includes” it performs will fail. And since I do not want any notice on that, I put an “@” sign in front of the corresponding “include” directive.
Everything is nice so far, the “file not found” notice is suppressed. BUT, funny enough, it seems that all error messages in the included file are being suppressed too !
Very annoying, does anyone know about this?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 14.2009 — Wouldn't the easiest method be to test if the [url=http://php.net/file_exists]file_exists[/url] or [url=http://php.net/is_readable]is_readable[/url] first?
Copy linkTweet thisAlerts:
@deck42authorApr 14.2009 — Yes, that would be a workaround.. I will perhaps end up doing that but still I am curious if the problem I have is known to exist or just some weird bug that can't be (easily) reproduced.
Copy linkTweet thisAlerts:
@scragarApr 14.2009 — It's not a bug, @ is the error suppression operand, it, unfortunately, operates recursively, applying not just to the call it is used on, but any children.

For reference I wrote this function I while ago for the purposes of my own code:
[code=php]function inc($f, $p=false){
if(substr($f, 1, 1) == '/'){ // absolute path
if( ! file_exists($f) || ! is_readable($f))
return false;
include $f;
return true;
}
if($p === false)
$p = explode(':', ini_get('include_path'));
if(!is_array($p))
$p = array($p);
foreach($p as $i){
if(file_exists($i.DIRECTORY_SEPARATOR.$f) && is_readable($i.DIRECTORY_SEPARATOR.$f)){
include $i.DIRECTORY_SEPARATOR.$f;
return true;
}
}
return false;
}


// USAGE:
if( inc('file.php'))
echo 'file exists';
else
echo "file doesn't exist";[/code]

I have never had the chance to test this on a windows system, but I don't think the first part will work, since windows absolute paths don't look like that, I assume a fix however would look rather ugly.
Copy linkTweet thisAlerts:
@deck42authorApr 14.2009 — Alright thanks for the answer!
Copy linkTweet thisAlerts:
@NogDogApr 14.2009 — In most cases "@" is a poor solution to whatever problem it is being applied. In the vast majority of cases a bit more in the way of defensive programming will remove the need for it, and proper setting of the display_errors configuration (i.e.: it should be turned off) in the production version will obviate its use in any case. So instead of considering a file_exists()/is_readable() check to be a work-around, I would instead consider it to be good programming.

(I have been known to use "@" in a few places such as mysql_connect() calls where I don't want to risk having my database login credentials displayed should it fail and display_errors is turned on for some reason.)
×

Success!

Help @deck42 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...