Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @include("filename") mean? What's the difference between that and include "filename"? [duplicate]

Tags:

php

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I'm making a web application that uses URL queries to access different parts of the application. I was looking for a solution to make an invalid query like index.php?page=dashboarrrd display an error 404 message instead of a PHP error.

After some searching, I found that I could use something like the following to do the job:

if(!@include($fileName)){
    @include("pageData/404.php");
}

And that makes sense, but I don't know why that works. I mean, what the heck does the @ before the include mean? I totally understand include $filename; but I need an explanation for @include ($fileName)

like image 988
Titus Avatar asked Nov 28 '22 10:11

Titus


1 Answers

the code you really need is

$fileName = "pagedata/".basename($_GET['page']).".php";

if(is_readable($fileName)) {
    include($fileName);
} else {
    include("pagedata/404.php");
}

and @ has absolutely nothing to do here

@ is one of biggest delusions coming from lack of experience.
Ones who using it do expect only one kind of error, while in fact there can be many more. And to gag ALL possible messages to suppress only one of them is definitely like to throw out the child along with the bath.

There is a fundamental problem that makes such misunderstanding so widespread:

Most PHP users cannot distinguish three sides of error control:

  1. error handling
  2. error reporting
  3. user notification.

Most of time in sake of [3] people mess with (1) and (2). While each of them require separate treatment:

  1. your program should raise no intentional errors. No error should be part of program logic. All errors that ever raised should be only unexpected ones.
    if you expect some error, you have to handle it. Not gag with @, but gracefully handle. is_readable() in my code exactly for that.
  2. error reporting is for the programmer and should be always at max. So, error logging should be enabled on a live site and a programmer have to check all errors occurred. And of course he would be interested in such errors, thus @ will do only harm here.
  3. User-level error messages should be different from system ones. Your 404.php is a good example of such user-friendly behavior. As for the system error messages, a user shouldn't be able to see them at all. Just turn display_errors off and see - there is no use for the @ again!
like image 162
Your Common Sense Avatar answered Nov 30 '22 23:11

Your Common Sense