SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP's other control structures

    I recently learned about PHP's other control structures

    PHP Code:
    if(condition == true):
    // code
    endif; 
    instead of

    PHP Code:
    if(condition == true) {
    // code

    I honestly prefer the vb method slightly better as it makes code easier to read (finding a closing bracket from a if statement is much easier), however I develop applications, an I've got no idea from what version this style was implemented. Ive looked on the PHP website, however I can't find a thing.

    Does anyone know about this style? Any links would be most helpful

    Bling

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Addict loadx's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne, VIC, Australia
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well thats nice to know but i think i'll keep to the older method.. i dont really see how using a semicolon is an advantage over a brace :S

    oh well

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In terms of size the brace has the advanage (in terms of execution), but only slightly.

    But look at this following example.

    PHP Code:
    <?php

        
    if():
        
            while():
            
                switch():
                
                    foreach():
                    
                        if():
                        
                            while():
                            
                                switch():
                                
                                    foreach():
                                    
                                    endforeach;
                                
                                endswitch;
                            
                            endwhile;
                        
                        endif;
                    
                        if():
                        
                            while():
                            
                                switch():
                                
                                    foreach():
                                    
                                    endforeach;
                                
                                endswitch;
                            
                            endwhile;
                        
                        endif;
                    
                    endforeach;
                
                endswitch;
            
                if():
        
                    while():
                    
                        switch():
                        
                            foreach():
                            
                                if():
                                
                                    while():
                                    
                                        switch():
                                        
                                            foreach():
                                            
                                            endforeach;
                                        
                                        endswitch;
                                    
                                    endwhile;
                                
                                endif;
                            
                                if():
                                
                                    while():
                                    
                                        switch():
                                        
                                            foreach():
                                            
                                            endforeach;
                                        
                                        endswitch;
                                    
                                    endwhile;
                                
                                endif;
                            
                            endforeach;
                        
                        endswitch;
                    
                    endwhile;
                
                endif;
            
            endwhile;
        
        endif;

    ?>
    A bit extreme. Yes, however there are only 6 if statements, which means only 6 endif();

    However using the traditional brace, there is over 2 dozen end braces }

    Even if the code is properlly indented, it just makes it a bit easier to read your code.

  5. #5
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I don't like the 'alternative style' much: At an end*; I need a blank line to make it easier to spot ends of blocks of code within code. With braces, I do not seem to have that problem; the } character is small enough and when put on a line of it's own, it almost functions as a blank line itself.

    I honestly prefer the vb method slightly better as it makes code easier to read (finding a closing bracket from a if statement is much easier),
    You could find a matching brace in all but the most useless (notepad *cough*) of editors.

    Anyway, probably all very subjective, I guess.
    Per
    Everything
    works on a PowerPoint slide

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The : alternative style can be useful when you are creating php files as templates, i.e.

    PHP Code:
     some html
    <?php if (true == $admin): ?>
    <p> you are the admin </p>
    <?php endif; ?>
    some more html
    might look a little cleaner in the designer's eyes than:

    PHP Code:
     some html
    <?php if (true == $admin) { ?>
    <p> you are the admin </p>
    <?php ?>
    some more html
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Addict Mower's Avatar
    Join Date
    Feb 2004
    Location
    Aussie Aussie Aussie
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both good points.



    I think the key here is that the standard is to use the C style over the vb style. Also I find it easier to read the brace version. A good editor will also help a lot in controlling the braces for you.



    At the end of the day it comes down to what is "CooL" and I would say they that php community at large does frown upon vb style


    "Will I ever find my way to GURU?"

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think Jason's comment may be a little stroll down PHP memory lane. PHP probably started out much like the templating systems we have built today. But now that we have come full circle and are using templating systems to compile to PHP (e.g. WACT) the old style actually reads better for that use.

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even if you have a good editor (I use ultraedit), when you have 5000+ lines of code for one function it just makes it that much easier. However even raising this issue i still use braces and I probally still will, however its always handy to know.

    But no matter what style you choose stick to it. Don't change your coding style half way though an application (this is how I actually found out about this style of control structures when another programmer changed styles).

    What would make reading code alot easier if you could minimise code blocks. I haven't seen an editor like this before. (See the sample at http://www.softcomplex.com/products/tigra_tree_menu/). If you could minimise blocks of code (using the plus/minus symbol), you could easily navigate your code.

    Does anyone know of an editing program that does this?
    Last edited by Bling; Oct 18, 2004 at 02:43.

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    UK
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bling,

    I use Notepad++ - it's a lot better than its name. It also supports the expandable/collapsable code and is open source.

    http://notepad-plus.sourceforge.net/uk/about.php

  11. #11
    SitePoint Addict loadx's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne, VIC, Australia
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    StefanH: Brilliant editor man im just admiring the screenshots now deffinately gunna give this baby a look, the zoom in feature in particular i think would be really handy..

    Only query i have, can i have multi documents on multi screens? I don't run a stretched desktop for my daul monitors but rather fixed windows to each screens resolution. If im not able to do this then can i run multiple instances of the application.?

    If you don't know then ill download it and have a gander and post my findings so everybody else can be aware

    Thanks heaps.

  12. #12
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Bling
    I honestly prefer the vb method slightly better as it makes code easier to read (finding a closing bracket from a if statement is much easier), however I develop applications, an I've got no idea from what version this style was implemented. Ive looked on the PHP website, however I can't find a thing.
    It could also be the placement of your braces that causes readability problems. For example, you used this in your example:
    PHP Code:
    if (condition) {
      
    //code

    I tend to go for this style:
    PHP Code:
    if (condition)
    {
      
    //code

    That way, the braces always line up. Even in longer and nested code chunks, this remains pretty easy to read:
    PHP Code:
    if (condition)
    {
      while (
    true)
      {
        if (
    anotherCondition)
        {
          
    //code
        
    }
      }

    The braces match up just by looking up and down the columns.

  13. #13
    SitePoint Addict loadx's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne, VIC, Australia
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok after a short look at the editor my questions have been solved

    1) No notepad++ cannot open mutiple instances of itself
    2) Opening 2 documents 1 on eahc monitor.. ok hackishly you can clone a viewport and then stretch the crap outta the application and therefore align the 2 documents side by side.. so overall yes you can but its pretty messy.

    Sorry to keep dragging the thread more off topic.. Anyway included is a screenshot of some PHP code being stretched between 2 monitors, resized so it loads faster and its only a crappy gif but yeah you'll get the idea.



    if anyone does happen to know of any editors that do both things i asked can you please get back to me oh and dont say notetab pro with some fancy addons i want most features that notepad++ was offering

    Features i require that notepad++ did offer:

    +expandable code by brace
    +zoom in
    +cloning documents to have them side by side on one monitor.

  14. #14
    SitePoint Zealot tezza's Avatar
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up PHP Eclipse does everything you could resonably want

    Quote Originally Posted by loadx
    Features i require that notepad++ did offer:

    +expandable code by brace
    +zoom in
    +cloning documents to have them side by side on one monitor.
    not sure what you mean by zoom in.
    I use and highly recomend php-eclipse. Naturally you will need the Eclipse IDE Platform first (get the latest snapshot). Get the latest php-eclipse plugin for it. I think it will blow you away, I was pretty impressed with the latest version. It's all a bit bleeding edge though and can get it's knickers in a knot sometimes, but bear with it, the project is extremely active and I'm sure they will iron out the wrinkles.

    The eclipse platform has the built-in ability to open as many window instances as you like - thereby you have the ability to have the same script side-by side (although this is less practical without a dual-monitor setup). "Code folding" is based on functions/classes/methods. There is also a handly "outline" view which I use all the time. And of-course, being Java-based, eclipse will run on Windows/Linux/Mac/Solaris no problems at all. Eclipse also ships with built-in CVS integration. It features a truckload of other plugins and you can download a truckload more. PHP-Eclipse parses all your code for JavaDoc style comments and displays them in a tooltip when you hover over a variable or function - works for internal PHP functions too.

    Oh yeah, and there is no excuse to complain about finding braces either. Placing your cursor next to a brace|[round|square]bracket will highlight it's other. It does realtime scanning as you type and will underline parse errors like Visual Studio.NET does. So you if you get lost in your braces, you soon know about it.

    PHP-Eclipse has a host of other features I've never really explored - some with links to Apache and MySql. It also has some sort of support for PHP Unit. It also uses the PHP5 parser by default (this may be due to the fact that I pointed the scanner to my PHP5 CLI binary).

    It's worth menioning that I have used Eclipse since before PHP-Eclipse was born (for my Java work and it's CVS integration). The speed of growth of the PHP-Eclipse plugin has been stunning. It now has a feature set so big, no developer will use all of it. I believe the reason for this success is due to the absolute quality of design of the Eclipse platform. As a modular framework, it is designed beautifully. It's basiclally the software behind IBM's Websphere studio (it's commercial due to a host of [J2EE related] plugins). I believe the offical JBoss editor is implemented as an Eclipse plugin. A couple of years ago it was estimated that IBM sank $40million into it's development. Hence, implementing automatic to-do lists and code outline views was trivial - just a matter of using the existing API calls.

    http://www.phpeclipse.org
    http://www.eclipse.org

    All this software is free.


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
  •