JBS103 Posted February 15, 2007 Share Posted February 15, 2007 I have a quick question (that requires a quick answer) in terms of what is considered the best/cleanest/most proper way to do something. When checking if a variable is set, its my understanding that I can use this: <?php !$sqlConnect ? /*do something to exit*/ : true; ?> However, is that considered bad coding practice? Is a normal if-statement preferred over the ternary method (especially when the "else" part of this statement doesn't do anything)? <?php if(!$sqlConnect) { //do something to exit } I couldn't find a way to do a ternary without the "else" part, otherwise I would. Maybe its really obvious. I am only looking for a general answer and I have a feeling this is just a matter of opinion or style. I just want to make sure I am not committing some felony by doing something like this throughout my code. Thanks. Link to comment https://forums.phpfreaks.com/topic/38548-solved-general-coding-procedure/ Share on other sites More sharing options...
hitman6003 Posted February 15, 2007 Share Posted February 15, 2007 Generally speaking, ternary operations are used for assignment (the php manual's example does this). I would use a regular if statement for what you are describing because you aren't doing anything with the "else" portion of the ternary... if (!$sqlConnect) die("No database connection"); The curly brackets are optional with a "one-liner" Link to comment https://forums.phpfreaks.com/topic/38548-solved-general-coding-procedure/#findComment-185116 Share on other sites More sharing options...
JBS103 Posted February 15, 2007 Author Share Posted February 15, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/38548-solved-general-coding-procedure/#findComment-185123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.