FUNKAM35 Posted March 2, 2011 Share Posted March 2, 2011 I am trying to improve my php code and speed it up as there were a lot of echos and lines. Do I need the following snippet or can I delet it, I tried deleting it and page works fine, but di I need it, just thought its another line that could be eliminated??? else { echo "connection failed."; } Quote Link to comment https://forums.phpfreaks.com/topic/229363-do-i-need-connection-failed/ Share on other sites More sharing options...
Nuv Posted March 2, 2011 Share Posted March 2, 2011 I think you can.It just tells you when the connection is failed. However, it will hardly make a difference to speed. Quote Link to comment https://forums.phpfreaks.com/topic/229363-do-i-need-connection-failed/#findComment-1181788 Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 Not that simply echoing things like 'connection failed' is good error handling in the first place, but removing error handling will make your code worse, not improve it. You should be trying to catch as many possible error conditions as you can. Quote Link to comment https://forums.phpfreaks.com/topic/229363-do-i-need-connection-failed/#findComment-1181796 Share on other sites More sharing options...
ZacTopher Posted March 2, 2011 Share Posted March 2, 2011 Yes you can, but I would leave it and replace the echo with an error log function. Edit: You beat me to it thorpe Quote Link to comment https://forums.phpfreaks.com/topic/229363-do-i-need-connection-failed/#findComment-1181797 Share on other sites More sharing options...
PFMaBiSmAd Posted March 2, 2011 Share Posted March 2, 2011 In your error checking (1), error reporting/logging (2), and error recovery logic (3), you should - 1) Always check if something worked or failed, 2a) When something fails, output a user message (i.e. the action you tried on this page cannot be performed due to a server error...), 2b) When something fails, log all the relevant information about the error so that you know what is happening and you can find and fix the problem. 3) Recover from the error gracefully by not executing follow-on code that is dependent on the result of something that has already failed so that you don't produce a string of follow-on errors or send output to the web page that is broken... For the example code that you posted with the "connection failed." logic. Suppose that something happens out of your control, such as your database server is failing intermittently or you are exceeding the number of allowed connections to your database server. You would want to 1) check if your database connection worked or failed, 2a) Display some message to your visitor so that he is not faced with a blank page, 2b) log everything about the error, and 3) recover from the error by displaying the remainder of your page correctly but not executing all the code that was dependent on that database connection. Quote Link to comment https://forums.phpfreaks.com/topic/229363-do-i-need-connection-failed/#findComment-1181803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.