if ($you == "PHP Expert"){ echo $answer; }

Discussion in 'PHP' started by dp-user-1, May 7, 2007.

  1. #1
    Like the title? ;)

    Question: How come some conditional statements are formatted like this?
    if (!empty($blah)){
       echo "bleh meh";
    }
    PHP:
    And some like this?
    if (!empty($blah))
       echo "bleh meh";
    PHP:
    Thanks,
    Peter
     
    dp-user-1, May 7, 2007 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    because if u have more than 1 thing to do :

    ex :

    if (!empty($blah)) {

    echo "bleh meh";
    echo "bleh meh2";
    echo "bleh meh3";
    echo "bleh meh4";
    echo "bleh meh5";
    echo "bleh meh6";

    }

    You should use brackets , if its only 1 call , you can put it as the second ..
     
    commandos, May 7, 2007 IP
  3. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #3
    Just as commandos has already said.

    In my personal opinion it's better practice to just use {} all the time but opinion may vary on this one...
     
    Darkhodge, May 7, 2007 IP
  4. Andy Peters

    Andy Peters Peon

    Messages:
    430
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If nothing else it makes it easier to understand what is where

    if (A=="1")
    {
    echo "A = 1";
    }
    elseif (B=="1")
    {
    echo "B = 1";
    }
    else
    {
    echo "C = 1";
    }
     
    Andy Peters, May 7, 2007 IP
  5. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's probably better practice to use brackets all the time. If you do this:

    if ($foo == $bar)
      baz(); 
    PHP:
    You may add a statement like this:

    if ($foo == $bar)
      baz(); 
      bom();
    
    PHP:
    without realizing that it will not do what you intended. An alternative is to use

    if ($foo == $bar) baz()
    PHP:
    to avoid that confusion. But whether this really matters varies from programmer to programmer.
     
    lemaitre, May 7, 2007 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    :) I figured it was something simple like that. Thanks for the responses!
     
    dp-user-1, May 7, 2007 IP