Jump to content

[SOLVED] General Coding Procedure


JBS103

Recommended Posts

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

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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.