SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: if else problem

  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if else problem

    Hi there nice place u got here with a lot of knowlegdable people guess thats why im here "DUMBO"!

    Im having a problem with the else section as i keep getting a parse error there and its something to do with the foreach statement cause if i kill that it works but obvious i need that

    Any help would greatly be appreciated

    PHP Code:
    if ($submit) {
    foreach (
    $fields as $field) {
             
    $control "ord_$field";
    if (empty($
    $control)) {
        $
    $field "red";
            }
        } else {
            echo 
    "boo";
            }


  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    PHP Code:
    if ($submit) {
       foreach (
    $fields as $field) {
          
    $control "ord_$field";
          if (empty($
    $control)) {
              $
    $field "red";
          }
          else {
             echo 
    "boo";
          }     
       }


  3. #3
    SitePoint Addict kevin_tremblay's Avatar
    Join Date
    Jul 2001
    Location
    Boston Area (North shore)
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like John got it for you, looks like an extra bracket in the statement.
    Kevin Tremblay-- Sys Admin
    work: kevin_tremblay@hsgmed.com
    "The object of the journey is not to arrive."

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is one reason why I tend to code like this:

    PHP Code:
    if ($submit) {
       foreach (
    $fields as $field) {
          
    $control "ord_$field";
          if (empty($
    $control)) {
              $
    $field "red";
          }
          else {
             echo 
    "boo";
          } 
    // end if
       
    // end foreach
    // end if 
    In smaller chunks it obviously doesn't help much but you can see how a misplaced (or left out) bracket can mess things up.

    It really helps me if I have a huge chunk of code which needs a closing bracket or if I have a large number of nested if's ( e.g. //end if( empty( $$control ) ) ) or large case statements:
    PHP Code:
      switch( $bob ) {

        case 
    '43':
          ..
          
    lots of statements
          
    ..
        break; 
        
    // end case '43'

        
    case '44':

      } 
    // end switch 
    So I guess I find it most useful on multi-screen statements in which I can't look at a } and immediately discern to whom it is closing.

    However, I always end *every* } with the // end whomever so I never forget. I know the ONE time I forget to // will be something important and cause me a ton of pain.

  5. #5
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MattR,

    Add comments is optional to every one (every one should do, in case a team to do a big program and each one must look the code of others). But indent the code is really the right way to do the things correct.
    php? mysql? apache? That's it.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed. I don't add comments on closing curly braces (I'd get annoyed with it all too quickly), but I do indent...usually only two spaces, however. Some kind of indentation is *crucial*.

  7. #7
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx guys for all ya help much appreciated


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
  •