...

View Full Version : Correct PHP syntax for nested if else statements



Relaxasaurus
02-08-2006, 06:40 PM
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:



<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?

GJay
02-08-2006, 07:25 PM
It's much easier to use separate echos:


<?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";
}
?>

Relaxasaurus
02-08-2006, 07:35 PM
thanks mayne, works like a charm!

maltrecho
02-09-2006, 08:44 AM
You could also use ternary operators:
(it looks cleaner a nicer in this cases from my point of view)


<?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";
}

?>

BaDgEr
02-09-2006, 08:59 AM
I was going to suggest the same as Maltrecho.

fail
06-02-2011, 09:05 AM
I have the same question and don't get it. This seems not to work as I wanted it to work:




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?

tomharto
06-02-2011, 09:44 AM
$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



$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

fail
06-02-2011, 09:53 AM
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.

tomharto
06-02-2011, 10:02 AM
If your check if one variable for 3 words you could use || (or) like this


$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

tangoforce
06-02-2011, 11:56 AM
I'd personally use a switch instead.

tomharto
06-02-2011, 12:51 PM
Could you post a quick example of a switch? I'd like to have a look at it too :)

Fou-Lu
06-02-2011, 02:17 PM
Could you post a quick example of a switch? I'd like to have a look at it too :)



$a = 4;
switch ($a)
{
case 1:
print '1';
break;
case 2:
case 3:
case 4:
print '> 1';
break;
default:
print 'Unknown.';
}

tomharto
06-02-2011, 02:23 PM
Thanks :). Now its time to figure out how that works :P

tangoforce
06-02-2011, 02:57 PM
The switch can have any sort of different case statement. It's a very useful tool:



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.

Fou-Lu
06-02-2011, 03:40 PM
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):


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.


$a = 4;
switch ($a)
{
case "4":
print '$a is 4';
break;
}

That will print that $a is 4.


I should clarify as well, I consider the loose comparison to be a negative.

tangoforce
06-02-2011, 06:18 PM
Apologies, i forgot the parenthesis..


switch($Var)
{
case (is_numeric($Var)):
//Do something
break;
}

fail
06-04-2011, 03:19 AM
Firstly thanks for not bashing me for digging up this old topic.

SWITCH is totally new to me and I can't see how I can that apply that in my case.

Instead I used the 'colon' IF/ELSEIF format:




if ($RandomNumber == '1'):
echo 'The number was 1';
elseif ($RandomNumber == '2'):
echo 'The number was 2';
elseif ($RandomNumber == '3'):
echo 'The number was 3';
else:
echo 'The number was 4';
endif;



For those (like me) who blindly copy note that you can have ONLY one line of code to be executed. Any more lines will be executed no matter what.

shadowmaniac
06-04-2011, 03:37 AM
switch($RandomNumber)
{
case 1:
echo 'Number is 1';
break;
case 2:
echo 'Number is 2';
break;
case 3:
echo 'Number is 3';
break;
case 4:
echo 'Number is 4';
break;
default:
echo 'Number s neither 1 nor 2 nor 3 nor 4';
break;
} // switch()


or better yet:


switch($RandomNumber)
{
case 1:
case 2:
case 3:
case 4:
echo 'Number is '.$RandomNumber;
break;
default:
echo 'Unkown number';
break;
} // switch()

SKY-ProToSs
06-04-2011, 05:01 AM
But that doesn't really matter either, it's probably just because you have echo's inside of echo's and confused the quotes somewhere. It's kind of hard to tell really.

Just more simple to structure it in an organized way element and then another element.




<div id="nav">
<ul>
<? if ($thisSection == "Home") {
echo "<li>You are home, browse sub-navigation below</li>";
echo "<div id=\"subnav\">";
echo "<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>";

//Notice what's happening. The echo's are all separate and the if elses wont mess anything up. You really only need the echo's for the output and the if elses for what li tags are displayed. It wont display the if elses or bug this way and it's organized!
echo "</div>";

} else {
echo "You are not home";
}
?>
</ul>
</div>



It's really this simple. You don't want to wrap everything in echo's just the code you want echoed. If you notice, it's a lot more organized this way and we AND you can tell where the quotes and semicolons go.

The echo statements wont have any impact on the if else statements this way. They will just either echo one li or the other. While the div and ul just echo.

Not sure why you'd have structure elements inside of an echo. Those will either work or they wont, so that confuses me.


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):


switch ($a)
{
case is_numeric($a):
print '$a is numeric';
break;
}

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

[/edit]

Does the function just generalize if we don't specify which variable?

I guess it would otherwise run as just a function, since you declared the function inside a variable and not the variable inside the function?

Fou-Lu
06-04-2011, 07:41 AM
Does the function just generalize if we don't specify which variable?

I guess it would otherwise run as just a function, since you declared the function inside a variable and not the variable inside the function?

No, it does not. Attempting to do something like this:


switch($a)
{
case is_numeric():
...
}

Will cause the is_numeric function to toss an error since its missing a required argument. Switch works on the value of a variable, not on what the variable is.
Switches are literally a shortcut route to:


if ($a == 1)
{
}
else if ($a == 2)
{
}
else if ($a == 3)
{
}
else
{
}

And just like the above, you cannot do as so in PHP:


if ($a == is_numeric())
{
}


Switches are designed for absolute resolution of a variable value, not for use in a general boolean comparison. Generalized comparisons like is_numeric can only be used in an if or while type comparison which is based on a boolean result and does not need to be a similar comparison. That is why I was saying that this is pointless:


switch ($a)
{
case is_numeric($a):
print '$a is numeric.';
break;
}

// When this is easier:
if (is_numeric($a))
{
print '$a is numeric';
}


IMO the best thing to do is never use a case from a function. This is a bizarre thing to do:


$a = 't';

function t()
{
return 't';
}

switch ($a)
{
case t():
print '$a is matched against t()';
break;
}

But you'll notice that it will match the returned value of t() against the value of $a. This is why this doesn't work as expected with is_numeric:


$a = null;
switch($a)
{
case is_numeric($a):
print 'wth how\'d I end up in here?';
break;
}


false = null = void = 0; a failed call to is_numeric == null, so that matches the switch of $a.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum