Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By sarahswansea

Thread: Opinion: PHP Style

  1. #1
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times

    Opinion: PHP Style

    ...or pretty much any C-like language, I suppose.

    Ran into this today, and I'm just looking for some opinions on coding style for such a case.

    I have a for loop that goes through a multidimensional array adding more info to each index. This is immediately followed by the return statement for the function. In this instance, completely readable because everything in the for loop is indented because it's part of that block.

    Fast forward, building a similar function with the same code, but with no loop, so the code isn't indented.

    Visually, the return statement kinda gets lost in with the rest of the code. Question is, in a situation like this, what would you do?

    I ended up just indenting the block as if it were in a loop.

    Side note: I know I should wrap up that code in a function, and I intend to, but this could happen in a situation where that's not practical.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  2.  

  3. #2
    Senior Member sarahswansea's Avatar
    Join Date
    Oct 2015
    Location
    Swansea, UK
    Posts
    152
    Member #
    52184
    Liked
    64 times
    At first glance you might hate this idea, but did you know the braces { } in PHP and C-like languages don't actually have to follow any special keywords? You can use them whenever you like to group code together:

    Code:
    <?php
    
    {
        /* a logical block of code goes here */
    }
    
    return $foo;
    
    ?>
    As a bonus, this is easily refactored then:

    Code:
    <?php
    
    function foo()
    {
        /* a logical block of code goes here */
    }
    
    return $foo;
    
    ?>
    In languages *other than* PHP, braces introduce a new local scope, which is much more sane and useful behaviour:

    Code:
    // C99
    
    function foo(void)
    {
        int bar=123;
    
        {
            int a = 1;
            // code using int a
        }
        
        {
            char a = '\n';
            // code using character a
        }
    
        // variable a no longer in scope
    
        return bar;
    }
    However in PHP, its only functions that introduce a new scope so really its no different to your example of just indenting.
    Ronald Roe likes this.

  4. #3
    Senior Member sarahswansea's Avatar
    Join Date
    Oct 2015
    Location
    Swansea, UK
    Posts
    152
    Member #
    52184
    Liked
    64 times
    This answer on StackOverflow has someone with similar reasoning and a better example than me too!

    Use curly brackets to structure code in PHP - Stack Overflow

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    I wondered if that was possible without causing scoping problems. PHP already scopes really weird. Interesting that it doesn't.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:45 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com