10

What’s your preference for a simple if without shorthand:

if ($variable == ‘test’) echo ‘’hello’;

if ($variable == ‘test’) { echo ‘’hello’; }

if ($variable == ‘test’) {
echo ‘’hello’;
}

Comments
  • 18
    Always the third. Clear, consistent, less error prone
  • 4
    This.

    if (condition) {
    // do something
    }
  • 4
    Always 3, but it depends. 1 can be good in some cases for readability.
  • 1
    1 for small quick things.
    But 3 is how it’s meant to be written (following the PSR)
  • 0
    puts "Hello" if condition
  • 7
    echo $variable=='test'?'hello': '';
    *hides and runs
  • 1
    1 if short oneliner.

    Long oneliner:

    If (gaaa)
    gàaaaaaaaaaaaaaaaaaa();
  • 0
    3 is the most legible
  • 1
    @cursee
    if (condition)
    {
    //Whatever
    }
  • 0
    @mojo2012 same for me unless its just a return. Then I actually prefer the 1 line in terms of readability.
  • 0
    4.
    if( bool)
    // do something
  • 2
    @erroronline1 😣 you joke, but I have inherited a project full of this.
  • 0
    if ($variable == "test")
    echo "hello" ;

    No {} since only one line.
  • 2
    @C0D4 well... erm... yeah... a joke... haha!
    serious. i know it is likely bad practice. but i am lazy and no one ever sees my code. i'd not recommend it to a learner though.
  • 2
    @erroronline1 that’s what my last cooworker said, now here I am 🤕🔫
  • 2
    @C0D4 i am sorry 😞
  • 1
    @erroronline1 it’s ok, just tell me you don’t use alt syntax as well ( something I thought died in PHP4 ) and I’ll forgive you when you start unraveling your ternary operators.

    http://php.net/manual/en/...
  • 1
    @C0D4 wow! i wasn't aware of this. although i am a slow learner i see this wouldn't end well...
  • 0
    3rd!!!
  • 0
    3rd.

    Also @C0D4, I use alt syntax when I place clear html between them, else I use that 3rd notation.
  • 0
    i loved first, but i have a pipeline with phpcs PSR2 standard test that made me use third...

    and honesly i didnt see anyone use second :)
  • 0
    $boolValue && someFunc ()

    Works great :-D
  • 0
    @C0D4 explain to me, whats bad about ternary operators?
  • 0
    @Codex404
    They lead to unclear operations very quickly, especially when people start nesting them and having multi logic paths and variable assignments occurring, and multiple outputs. This shit can’t be tested nor can it be easily followed to work out the actual expected behaviour.

    For single liners and the quick bool occurring I can live with them, but I’ve seen things I can’t bleach from my eyes.
  • 0
    @C0D4 but thats programming all together. If you abuse a tool its horrible.
  • 0
    if ("test" == $variable) { echo "hello"; }
    I am sure you know the reason ;)
Add Comment