www.webdeveloper.com
Results 1 to 5 of 5

Thread: trying to end a multi else if block...

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    trying to end a multi else if block...

    trying to end an a multiple elseif block

    i coded this elseif block according to the recommendations on php.net. but the last else: with the colon is generating an error.

    then, if i leave off the colon the following { throws an error.

    so i then tried making the last else an elseif but then endif; throws an error. very frustrating.

    i did try making this a case statement but then the booleans weren't working as expected

    PHP Code:
    if(($manIdAdd != 0) && (!isValidURL($urlAdd))) {
            echo 
    'Id is good but URL is bad, add to error array';
        }
        elseif((
    $manIdAdd == 0) && (isValidURL($urlAdd))) {
            echo 
    'URL is good but ID is bad, add to error array';
        }
        elseif((
    $manIdAdd != 0) && (isValidURL($urlAdd))) {
            echo 
    'Both are good';
            echo 
    'Run Q';
        }
        elseif((
    $manIdAdd == 0) && (!isValidURL($urlAdd))) {
            echo 
    'Both are bad which means user didn\'t intend to add so exit';
        }
        endif; 
    Last edited by toptomato; 05-25-2012 at 05:34 PM.

  2. #2
    Join Date
    May 2012
    Posts
    4
    Just replace the endif with an empty else { }, endif is improper syntax

    Code:
    if(($manIdAdd != 0) && (!isValidURL($urlAdd))) {
            echo 'Id is good but URL is bad, add to error array';
        }
        elseif(($manIdAdd == 0) && (isValidURL($urlAdd))) {
            echo 'URL is good but ID is bad, add to error array';
        }
        elseif(($manIdAdd != 0) && (isValidURL($urlAdd))) {
            echo 'Both are good';
            echo 'Run Q';
        }
        elseif(($manIdAdd == 0) && (!isValidURL($urlAdd))) {
            echo 'Both are bad which means user didn\'t intend to add so exit';
        }
        else { }

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    will do, thank you.

    this is from php.net though is obviously not correct

    PHP Code:
    /* Correct Method: */
    if($a $b):
        echo 
    $a." is greater than ".$b;
    elseif(
    $a == $b): // Note the combination of the words.
        
    echo $a." equals ".$b;
    else:
        echo 
    $a." is neither greater than or equal to ".$b;
    endif;

    ?> 

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,226
    There are two different PHP conditional syntaxes, one using the more common curly braces approach and the other using the colon and "endif" technique -- but you cannot mix the two together.

    Also, you don't need an else{} -- any other valid statement following the closing brace of the last elseif would also terminate the chain.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2010
    Posts
    281
    uhhh!, did not notice that (brackets not used), guess i'm tired.

    thanks for pointing that out, it's working fine.


    thanks NG,

    i like that format,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles