Results 1 to 17 of 17
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Keeping track of IF in PHP
    Is there a simple way to keep track of or layout code in PHP that will help me not lose my place when I have multiple nested IF statements in a PHP script.

    I seem to get myself hosed up once I get a few nested 'If' statements or 'While' loops and it frustrates me to waste time if there is something I am not doing correctly.

    Many thanks.
    Thanks,
    Doug

  2. #2
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Do you mean something to let you know where they end? Many editors will highlight the two tags that go together, or for if statements, the brackets. Same thing for while loops.

    Sometimes, I find that when I seem to need a bunch of if statements that possibly the code could be rewritten in a more direct way to get to the end result that I want. Maybe the select query could be narrowed. This definitely isn't always the case but it's worth a try.

    Also, I have learned that adding comments for the beginning (and ending) of specific bits of code is extremely helpful.

  3. #3
    Full Member
    Join Date
    October 22nd, 2006
    Posts
    200
    Yes there are two ways one in indenting the second is commenting

    PHP Code:
    <?php 
    //if merchant
    if (merchant=="abc"){
          
    merchant code goes here
         
    //if product
         
    if (product=="def")/{
                
    product code goes here
         
    }//endif product
         
    more merchant code
    }//endif merchant 
    ?>
    I use one tab for each level.

    In dreamweaver you can click ctrl-shift-> to indent your code.

    Bob
    Last edited by Donk; April 19th, 2008 at 11:37 AM. Reason: spellling

  4. #4
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    What bettylou said! I'm using phpDesigner 2008 and it is real nice for that. You click on one } and it shows in green the { that goes with it.

    It high lites a line with a syntax error right away and really saves time hunting them down.


  5. #5
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Wow great thanks for the rapid response. I use DW but might thinking about an editor just for PHP.
    Thanks,
    Doug

  6. #6
    Full Member Code Monkey's Avatar
    Join Date
    June 11th, 2007
    Posts
    337
    I use Notepad ++ for all of my PHP coding..

    If you highlight a } it will also highlight its {.. Very handy..

    And it's FREE!

  7. #7
    Full Member
    Join Date
    October 22nd, 2006
    Posts
    200
    Dreamweaver is excellent for php

    From version 8 it too has the ability to highlight any thing between {} .

  8. #8
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    I use DW, love it..

    But for effective visual "bracketing", check out jEdit... it's terrific, also free! http://jedit.org/

  9. #9
    ABW Ambassador
    Join Date
    January 18th, 2005
    Posts
    4,423
    Follow what Donk said, that is pretty standard. Other "cheats" aren't going to help you in the long run code in a more structured manner.

    Learning to code in a structured way will help you maintain your code. Spacing and indenting in a logical manner lets you quickly scan the code when making changes to keep the integrity of your brackets and logic.

    If you have this question I am guessing you are not currently very structured. Reading up on coding practices will go a long way to making your life easy in the long run. Here is a handy page http://www.gamedev.net/reference/art...article819.asp but check around, I am sure there are more.

  10. #10
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    I'm with chet and Donk. All about indenting and commenting. I use vim (unix-style text editor) and it's got a great feature for indenting. You can tell it to match-indent a series of lines by pressing == in command mode. What I like to do is select the lines I want indented, press yy to yank which tells me how many lines I've selected. Then I'll type "23 ==" for example to make the following 23 lines match the indent of the line above. This is helpful when you add or remove an all-encompassing if-conditional and need to modify the indents of everything inside. Since I've been doing this all my code is very neat and tidy.

    For comments I always head them up with a bar like /*********/ with 80 *'s to separate out sections of code. In vim I create this bar by typing "/ *esc* 80 i */" That'll create the bar without actually typing 80 individual *'s.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  11. #11
    ABW Ambassador Daniel M. Clark's Avatar
    Join Date
    January 7th, 2006
    Location
    Houston, TX
    Posts
    2,082
    You can never, ever do too much commenting. Even if you think it's not necessary to comment a certain bit of code, try to get in the habit of doing it anyway. Personally, I like to create blocks of code like this (little chunk from a project of mine):

    // ----------------------------------------------------------------------
    // set image size
    // ----------------------------------------------------------------------
    if ($imgRadio == 'img350') {
    $imageWidth = '350';
    }
    else if ($imgRadio == 'img650') {
    $imageWidth = '650';
    }
    else if ($imgRadio == 'img150') {
    $imageWidth = '150';
    }
    // ----------------------------------------------------------------------
    Daniel M. Clark
    Tech Manager
    Greg Hoffman Consulting

  12. #12
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by HecticDMC
    You can never, ever do too much commenting.
    Totally agree. Especially in the complicated bits. I sometimes have paragraphs of comments explaining to myself what I did. They come in very handy later when I come back to add to the code. For example:

    Code:
    /********************************************************************************/
    // Because the avg_score only gets inserted into $brand_tags
    // for the prices mode we need to take the score directly 
    // from $score_array.  This is because $brand_tags gets
    // set first for reviews and we don't want prices to overwrite it
    But sometimes I have to read these a few times to understand what the heck I'm talking about..

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  13. #13
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    Mine goes kind of like Donk's except that I copy/past the entire if to the ending comment:

    PHP Code:
    <?php
    //if merchant
    if (merchant=="abc"){
          
    merchant code goes here
         
    //if product
         
    if (product=="def")/{
                
    product code goes here
         
    }// if (product=="def")
         
    more merchant code
    }// if (merchant=="abc")
    ?>
    ---
    This response was masterly crafted via the fingers of Patrick Allmond who believe you should StopDoingNothing starting today.
    ---
    Focus Consulting is where I roll | Follow @patrickallmond on Twitter
    Search Engine Marketing | Search Engine Optimization | Social Media | Online Video

  14. #14
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by patrick24601
    Mine goes kind of like Donk's except that I copy/past the entire if to the ending comment:

    PHP Code:
    <?php
    //if merchant
    if (merchant=="abc"){
          
    merchant code goes here
         
    //if product
         
    if (product=="def")/{
                
    product code goes here
         
    }// if (product=="def")
         
    more merchant code
    }// if (merchant=="abc")
    ?>
    Sounds to me like a good way to keep a history of what the if-conditional was originally.. I just know I'd never keep the closing comment up to date.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  15. #15
    Full Member iolaire's Avatar
    Join Date
    October 3rd, 2006
    Location
    Arlington, VA
    Posts
    229
    What about moving more code out into sub files. In my ruby code I'm using more and more lib files to load/create/modify objects rather than having 1,000's of lines of code in one file.

  16. #16
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    That is always good. If you can wrap your mind around the concepts going toward object oriented programming is great in PHP. That way you can only include what you need and only when you need it. Combine that with the __autoload() function and you can really cut down on the size of your individual files.
    ---
    This response was masterly crafted via the fingers of Patrick Allmond who believe you should StopDoingNothing starting today.
    ---
    Focus Consulting is where I roll | Follow @patrickallmond on Twitter
    Search Engine Marketing | Search Engine Optimization | Social Media | Online Video

  17. #17
    Full Member markschok's Avatar
    Join Date
    January 18th, 2005
    Posts
    269
    If it's mainly IF statements your trying to keep rack of, would a Case: statement be better to use? I find this is sometimes better suited depending on the circumstances and much easier to keep track of.

  18. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Script (PHP?) to track IP and not show same content to same IP twice.
    By Hardaka in forum Programming / Datafeeds / Tools
    Replies: 10
    Last Post: February 15th, 2012, 01:37 PM
  2. ACX Fusion - Keeping track of Commissions
    By KimmyS in forum Marketing Resources & Power Tools
    Replies: 2
    Last Post: November 24th, 2008, 03:37 PM
  3. Keeping Track of them all!
    By CustomersFirst in forum Couponer's Corner
    Replies: 10
    Last Post: March 2nd, 2008, 10:47 PM
  4. Keeping track of everything
    By Javi in forum Midnight Cafe'
    Replies: 2
    Last Post: December 29th, 2006, 02:51 PM

Posting Permissions

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