visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Supressing error messages


By: Bernard France  Date: 15/11/2004 08:43:32  English  Points: 70 Status: Answered
Quality : Excellent
Say if I have an error on my page that a user is trying to access like a file was not included twice or a class already exists or a parse error or something, instead of showing the user a blank page or the php errors, I would like to instead save the error messages to a log file and give them a page saying that the page is currently being updated or down at the moment. Is this possible? ...And if so, how?
By: VGR Date: 15/11/2004 08:47:30 English  Type : Answer
put a "@" before the call in question, e.g.

$fd = @fopen("file.txt",'r');

this won't generate any error messages.

You can also switch off error reporting completly:

error_reporting(0); // Turn OFF error reporting

//Here comes the code that will not give errors

error_reporting(2039); // Turn ON error reporting

Generally, turning error reporting off is a very bad idea. If there is an error, your script will terminate for no reason at all.

The best pattern to use is, if you are doing something that may result in an error, put a @ before the statement, and then check for the success of the operation and handle the error yourself.

$fd=@fopen('file.txt','r');
if (!$fd) {
// here treat the error case
echo ("Couldn't open the file file.txt");
} else {
// here access was granted
// do something with the file...
}

Always check in the online documentation which kind of return value is sent by the function you want to use. Usually, but not always, it's a Boolean. If you fell on a function returning the infamous "a non-boolean value OR FALSE in cas of failure", then use the operator === or !== to test the returned value against FALSE.

Regards

Do register to be able to answer

EContact
browser fav
page generated in 291.250940 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page