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
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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.