Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Correct PHP syntax for nested if else statements

    Hi guys,

    I want to create an if else statement within an echo like this:

    <? if (condition) == "blah") {
    echo "Hello there " *insert nested if else statement here* ", welcome to our web site!";
    } else {
    echo "Please login.";
    }

    Actual code:

    Code:
    <div id="nav">
    <ul>
    	<? if ($thisSection == "Home") {
    		echo "<li>You are home, browse sub-navigation below</li>
    			<div id=\"subnav\">
    				<ul>"
    				if ($thisSubsection == 'Aboutus') { 
    					echo '<li><img src='images/about_active.gif'></li>';
    					} else { 
    					echo '<li><img src='images/about.gif'></li>'; }
    				if ($thisSubsection == 'History') { 
    					echo '<li><img src='images/history_active.gif'></li>';
    					} else { 
    					echo '<li><img src='images/history.gif'></li>'; }
    				"</ul>  //this line and the next are ignored
    			</div>";
    		
    		} else {
    		echo "You are not home";
    		}
    	?>
    </ul>
    </div>
    I guess I want to add an if else statement within an echo, then continue the echo by closing the <ul> and <div> tags at the end, but I'm having problems continuing the echo after the nested if else statements. Is my syntax off?
    Last edited by Relaxasaurus; 02-08-2006 at 06:46 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    It's much easier to use separate echos:
    PHP Code:
    <?php
    if ($thisSection == "Home") {
      echo 
    "<li>You are home, browse sub-navigation below</li>
      <div id=\"subnav\">
      <ul>"
    ;
      if (
    $thisSubsection == 'Aboutus') { 
        echo 
    '<li><img src='images/about_active.gif'></li>';
      } else { 
        echo 
    '<li><img src='images/about.gif'></li>'
      }
      if (
    $thisSubsection == 'History') { 
        echo 
    '<li><img src='images/history_active.gif'></li>';
      } else { 
        echo 
    '<li><img src='images/history.gif'></li>'
      }
      echo 
    "</ul></div>";

    } else {
      echo 
    "You are not home";
    }
    ?>

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks mayne, works like a charm!

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could also use ternary operators:
    (it looks cleaner a nicer in this cases from my point of view)
    PHP Code:
    <?php

    // condition ? true : false;

    if ($thisSection == "Home") {

      echo 
    "<li>You are home, browse sub-navigation below</li>
            <div id=\"subnav\">
            <ul>
            <li><img src=\"images/"
    .(($thisSubsection == 'Aboutus') ? 'about_active.gif' 'about.gif')."\"></li>
            <li><img src=\"images/"
    .(($thisSubsection == 'History') ? 'history_active.gif' 'history.gif')."\"></li>
            </ul>
            </div>"
    ;

    } else {

      echo 
    "You are not home";
    }

    ?>

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was going to suggest the same as Maltrecho.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have the same question and don't get it. This seems not to work as I wanted it to work:


    PHP Code:
    if (condition

    statement }

    ELSE {

    if (
    condition

       {
       
    statement
       
    }

    ELSE {
    if (
    condition

    {
    statement
    }

    ELSE {

    statement

    }  // else 3
    }  // else 2
    }  // else 1 

    What is the correct way to write it?

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    PHP Code:
    $a 0;
    if (
    $a == "1") {
        echo 
    "A is 1";
    }
    else {
        if (
    $a == "2") {
            echo 
    "A is 2";
        }
        else {
            if (
    $a == "3") {
                echo 
    "A is 3";
            }
            else {
                echo 
    "A isnt 1,2 or 3";
            }  
    // else 3
        
    }  // else 2
    }  // else 1 
    How about that? Or you could use elseif

    PHP Code:
    $a 0;
    if (
    $a == "1") {
        echo 
    "A is 1";
    }
    elseif (
    $a == "2") {
         echo 
    "A is 2";
    }
    elseif (
    $a == "3") {
        echo 
    "A is 3";
    }
    else {
         echo 
    "A isnt 1,2 or 3";
    }  
    // else 1 
    Last edited by tomharto; 06-02-2011 at 09:49 AM.

  • Users who have thanked tomharto for this post:

    fail (06-02-2011)

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I will need to test that.

    I need it coz I check a text for keywords. If the first search-word is NOT found (and only then) I check for the next one. I have 3 words I am checking.

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    If your check if one variable for 3 words you could use || (or) like this

    PHP Code:
    $a 0;
    if ((
    $a == "1") || ($a == "2") || ($a == "3")) {
        echo 
    "A matches the words";
    else {
         echo 
    "A doesnt match the word";
    }  
    // else 1 
    I dunno if that's better for what you need

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    I'd personally use a switch instead.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Could you post a quick example of a switch? I'd like to have a look at it too

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by tomharto View Post
    Could you post a quick example of a switch? I'd like to have a look at it too
    PHP Code:
    $a 4;
    switch (
    $a)
    {
        case 
    1:
            print 
    '1';
            break;
        case 
    2:
        case 
    3:
        case 
    4:
            print 
    '> 1';
            break;
        default:
            print 
    'Unknown.';


  • #13
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Thanks . Now its time to figure out how that works :P

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    The switch can have any sort of different case statement. It's a very useful tool:

    PHP Code:
    switch ($Var)
       {
       case 
    true://Boolean
          //Do something
          
    break;
       case 
    is_numeric()://Function which returns true
          //Do something
          
    break;
       case 
    'string'://A string
          //Do something
          
    break;
       case 
    5://Integer
          //Do something
          
    break;
       default:
    //Default for anything not matched by a case
          //Do something
          
    break;
       } 
    Note that default is optional, and several cases can be grouped together as Fou-Lou has shown with 2,3 & 4. If you don't use break at the end of a cases code block then the switch will continue to execute after that block of code. In this case after the case for is_numeric() had run, if there was no break then the case for 5 would have also run.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The switch won't work properly with a function call, unless that function call is non-parametrized (and then will not relate directly to the switched value). For that you need to continue using an if/else handle so that you can give it the variable (this is honestly pointless):
    PHP Code:
    switch ($a)
    {
        case 
    is_numeric($a):
             print 
    '$a is numeric';
             break;

    While it will work, its easier to just write if (is_numeric($a)).

    Logically the switch works on the value of a variable, not directly on it. Thats why we specify it for the case. PHP (unlike most languages) will actually allow strings in its switch statements.
    Also note that switches are loose in comparison.
    PHP Code:
    $a 4;
    switch (
    $a)
    {
        case 
    "4":
            print 
    '$a is 4';
            break;

    That will print that $a is 4.

    Edit:
    I should clarify as well, I consider the loose comparison to be a negative.
    Last edited by Fou-Lu; 06-02-2011 at 03:42 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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