Do php experts and professionals strongly recommend of using exit in every step? Recently, i found such recommendation in user notes but never read any thing like that in my books.
What step? You usually do exit() or die() after you do a header redirect, in case it doesn't work to stop from further execution. When certain errors occur, but otherwise, not much.
exit() and die() are to make sure the execution stops for whatever reason. I dont use more than a few times.
don't actually need the backets. Well, if you have code that is normally executed, but shouldn't be that time around (because, for example, you are redirecting), use exit, by all means. It's not a professionalism thing, more a convenience thing. And you don't really need exit after redirecting if there is no code following
I prefer a good and fully functional script than a faster one. I dont know what proffesionals do. but every working script is a good one.
Yeah, don't need (). I use it if I don't want to 'else' the rest of the page. For example if you have a page that runs a bunch of queries and later you add in a section to check the queries, you can if($query >> badterms){echo "go away!";}else{//rest of script} or (what I do): if($query >> badterms){echo "go away!";exit;} You can also use it when you're checking multiple errors and an unaccounted error should exit the script. EG: if($error == "code 1"){echo "Code 1!";} elseif($error == "code 2"){echo "Code 2!";} else{echo "Not sure what happened. Panicking!";exit;}
I recommend to use exit; when your debugging code and you need to check if your function is actually being invoked. Like if($var['something']) { echo "Were here"; exit; //stuff } PHP: Also use exit; after when you use a header() since you will want to stop any further output. I do not recommend (and probably others) to use echo & exit; for displaying error messages to a website visitor. It is unprofessional. Better to make your own error function and have a way to show the error in the page somwhere.
's good for debugging though. Naturally, when you actually use it, you should use your template engine or whatever it is you do instead of just showing a white screen with one sentence.
Good for debugging but make sure you don't leave it in the wrong places by accident. Non-execution of code later on could potentially mess you up.
Not the case at all. You can't account for every possible error, especially in a complex script. The exit example I posted should always be included or else you could have a runaway script.
Okay except for the most obscure error you might place echo "shouldn't happen" and exit;. I meant in parts of your script that handle say.. a registration form. You wouldn't want to be using echo and exit to tell them their username is already taken or their email is invalid etc. That would be really lame.
The exit() function at the end of your scripts is a good way how to prevent automatic bot hacks, because bots add their malicious code at the end of the scripts like index.php, main.php etc. The exit() function is a good counteraction.
Err, how about not get hacked in the first place? This is the first time I hear that as a suggestion.
mate it depends upong your coding steps. If you are not going to redirect you can even use return statement. your coding style should be wel formed so you can understand easily whats going on .