Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2006, 06:40 PM   PM User | #1
Relaxasaurus
New Coder

 
Join Date: Jun 2005
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Relaxasaurus is an unknown quantity at this point
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..
Relaxasaurus is offline   Reply With Quote
Old 02-08-2006, 07:25 PM   PM User | #2
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
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";
}
?>
GJay is offline   Reply With Quote
Old 02-08-2006, 07:35 PM   PM User | #3
Relaxasaurus
New Coder

 
Join Date: Jun 2005
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Relaxasaurus is an unknown quantity at this point
thanks mayne, works like a charm!
Relaxasaurus is offline   Reply With Quote
Old 02-09-2006, 08:44 AM   PM User | #4
maltrecho
Regular Coder

 
Join Date: Feb 2003
Posts: 345
Thanks: 0
Thanked 0 Times in 0 Posts
maltrecho is an unknown quantity at this point
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";
}

?>
maltrecho is offline   Reply With Quote
Old 02-09-2006, 08:59 AM   PM User | #5
BaDgEr
New Coder

 
Join Date: Jul 2005
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
BaDgEr is an unknown quantity at this point
I was going to suggest the same as Maltrecho.
BaDgEr is offline   Reply With Quote
Old 06-02-2011, 09:05 AM   PM User | #6
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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?
fail is offline   Reply With Quote
Old 06-02-2011, 09:44 AM   PM User | #7
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 796
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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..
tomharto is offline   Reply With Quote
Users who have thanked tomharto for this post:
fail (06-02-2011)
Old 06-02-2011, 09:53 AM   PM User | #8
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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.
fail is offline   Reply With Quote
Old 06-02-2011, 10:02 AM   PM User | #9
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 796
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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
tomharto is offline   Reply With Quote
Old 06-02-2011, 11:56 AM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
I'd personally use a switch instead.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 06-02-2011, 12:51 PM   PM User | #11
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 796
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Could you post a quick example of a switch? I'd like to have a look at it too
tomharto is offline   Reply With Quote
Old 06-02-2011, 02:17 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.';

Fou-Lu is offline   Reply With Quote
Old 06-02-2011, 02:23 PM   PM User | #13
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 796
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Thanks . Now its time to figure out how that works :P
tomharto is offline   Reply With Quote
Old 06-02-2011, 02:57 PM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 06-02-2011, 03:40 PM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:26 PM.


Advertisement
Log in to turn off these ads.