SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Location
    California, USA
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Markup design... why?

    I'm new to these forums and completely new to PHP. I'm currently working through Kevin Yank's "Build Your Own Database Driven Website Using PHP & MySQL", and am only on Chapter 3.

    I'm trying to understand the theory behind the way most php is written... as far as when a new line is created, where opening and closing braces are placed, etc.

    For instance, this first example looks more readable and understandable to me (especially the braces beginning and ending on the same line):

    Code:
    $firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    if ($firstname == 'Kevin' and $lastname == 'Yank')
       {echo 'Welcome, oh glorious leader!';}
         else
       {echo "Welcome to our website, $firstname $lastname!";}

    Why is it written like this (which seems confusing and a much easier way to miss an opening or closing brace)?

    Code:
    $firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    if ($firstname == 'Kevin' and $lastname == 'Yank') {
       echo 'Welcome, oh glorious leader!';
    } else {
    	echo "Welcome to our website, $firstname $lastname!";
    }

    I'm happy enough to simply learn the correct way to do it. I'm just curious what the reasoning is behind it.

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The best idea is to pick a well known coding standard and use that. Two of the most common:

    Zend Framework
    PEAR
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,805
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by Kirkus View Post
    I'm new to these forums and completely new to PHP. I'm currently working through Kevin Yank's "Build Your Own Database Driven Website Using PHP & MySQL", and am only on Chapter 3.

    Why is it written like this (which seems confusing and a much easier way to miss an opening or closing brace)?

    I'm happy enough to simply learn the correct way to do it. I'm just curious what the reasoning is behind it.
    I have recently started using this technique because it is Oh so much easier to see at a glance the opening and closing braces plus Notepad ++ editor matches the braces in a different colour when hovering over a brace:
    PHP Code:

      $firstname 
    $_REQUEST['firstname'];
      
    $lastname $_REQUEST['lastname'];
      if (
    $firstname == 'Kevin' and $lastname == 'Yank'
      {
         echo 
    'Welcome, oh glorious leader!';
      }else{
        echo 
    "Welcome to our website, $firstname $lastname!";
      } 

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using a convention from Symfony, i.e. each brace on new line:

    PHP Code:
    if ($hour >= && $hour <= 24)
    {
        echo 
    'Hi :)';
    }
    else
    {
        echo 
    'It\'s too late :(';


  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Putting braces on the same line only works if there is one statement in the middle. What happens when you have 200+ lines inside an if statement, or something like that?

    Putting the braces on separate lines makes it a lot easier to see what matches with what - if I see a closing brace, I can look up and see what if/for/foreach/while or whatever is inline with it. I put opening braces on the same line as the opening statement or loop, and closing braces on a separate line with a comment saying what opened it. Everything inside it is indented. Examples:

    PHP Code:
    if ($condition == "value") {
      
    somefunction();
      
    $somevar $someothervar;
    } else {
      
    //something else
    }//if

    foreach ($array as $var) {
      
    //something
    }//foreach 

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    The "correct" way is using a well documented standard for the language or project you're working on.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And they've all been documented on that Wikipedia page.

    Although code is already the documentation for (brace) code style.

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    It's a question of personal taste. I personally would do something like
    PHP Code:
    if($whatever == 'whatever') {
        
    /* do whatever
            ** */
    } else {
        
    /* do something else
           ** */

    whereas others will put the braces below. Its up to you and whatever you are comfortable with.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •