Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Which is more correct? { or :

    Right, now I like to keep PHP code as strict as possible.

    In most cases I have seen this done:

    if(condition){
    // Do whatever
    }

    However, I have also seen:

    if(conditon):
    // Do whatever
    endif;


    Can anyone tell me if one of these is more 'proper' than the other.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    One is not any more proper than the other. Many languages throughout history have/had slightly different syntax for control structures like the if/else blocks. PHP on the other hand allows you to choose which one you like. So it's more just a personal preference which one you feel more comfortable in using. Being a C++, Java programmer I usually use:

    PHP Code:
    if(condition){
        
    //yaks
    }
    else{
        
    //yaks

    since that is how it is done in those languages as well.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    + there are a few 'coding standards' around , and whilst they all have ways and means they almost all support the

    if($condition){
    }

    or

    if($condition)
    {
    }

    syntax , the older ':' method is generally frowned upon
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ok, thanks....

    Thanks for your help guys.


    P.S: Isnt

    if(condition){
    // Do whatever
    }

    and

    if(condition)
    {
    // Do whatever
    }

    the same? I always use the second and indent everything after a { so that it is easy to count if you miss out a }, but apart from the break, they look exactly the same.

    I have also seen people using one-line if statements:

    if(condition) // Do whatever


    Is this ok too, without the {}, or should you always use {}

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the code you're executing in the if statement contains only one line, then you don't need {}

    PHP Code:
    if($conditioncallFunction(); 
    is the equivalant to

    PHP Code:
    if($condition) {
    callFunction(); 
    Rule #1 - Admin is always right
    Rule #2 - If Admin is wrong, see rule #1

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    its not a case of whether it works or not (which is of course of primary importance ) but if you want to stick to a set of coding standards , most would suggest that even 1 liners should include the braces regardless of whether you actually need them or not, its basically about readability for other developers who may have to mess with your code at alater date.

    (From PEAR `standards`)
    You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.
    The closest thing to 'official' PHP coding standards can be found at PEAR .. http://pear.php.net/manual/en/standards.php

    Note that many do not give a hoot about PEAR perhaps with some justification , but as its now part of the 'official' release we may as well put up with it?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks for your help in this. It turns out that I wont be changing anything in my coding. Thanks for the info though!


  •  

    Posting Permissions

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