# if...else - in which order

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
this is probably an old topic:

if(a==1)
if(b==2)
echo "a is 1 and b is 2";
else
// what happens here?
echo "a is 1 and b is not 2";

The question is, where the else will react too. Usually it should be
the last if.

In general I put {} around just to be sure.

BR
Sonnich

## Re: if...else - in which order

Sonnich wrote:

Hi,

Use {} to note (to yourself and PHP) what you are doing.
The confusion you have is created by yourself. :-)

Try this:

if(a==1){
// a is 1
if(b==2){
// b is 2
echo "a is 1 and b is 2";
} else {
// b is not 2
echo "a is 1 and b is not 2";
} // end inner if-then-else
} // end outer if

Regards,
Erwin Moller

## Re: if...else - in which order

Sonnich wrote:

'else' clauses are always matched with the first unpaired 'if' preceding
them.

Using braces does make things clearer, though.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: if...else - in which order

Sonnich wrote:

It always matches the inner-most /if/ for which there are no interleaving
statements. (There is no "usually" about it.)

When nesting /if/s and using /else/, then it's certainly a good idea to
use the braces to indicate what is nested within what else. It will spare
your sanity a few months down the line if you have to edit that code again!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me  ~ http://tobyinkster.co.uk/contact

## Re: if...else - in which order

An noise sounding like Sonnich said:

if(a==1 && b==2)
{

}
elseif(a==1 && b!=2)
{

}
else....

etc.

Nesting if's just leads to confusion in the long run. Esp 2 months later when
you're looking back at something. Do it the way suggested above and you'll
help to avoid confusion, and will benefit from readability in your code too.

D.
--

/(bb|[^b])/
Trees with square roots don't have very natural logs.
What's the difference between ignorance and apathy? Who knows? Who cares?

## Re: if...else - in which order

.oO(David Gillen)

Not in properly structured and indented code. With some more braces the
OP's code is perfectly readable without any comment.

But I consider that personal taste.

Micha

## Re: if...else - in which order

Michael Fesser wrote:

I agree 100%.
Use {} everywhere to make things clear.
And also:
if (a==1 && b==2){

is a mess compared to:

if ((a==1) && (b==2)){

That are just a few extra () but makes the code completely clear and leaves
only 1 interpretation (for both PHP and humans who read the code).

Regards,
Erwin Moller

## Re: if...else - in which order

David Gillen wrote:

Purely a matter of personal preference.  Nested if statements can be
eminently clear if the code is properly indented.

For instance, I find

if (\$a==1) {
if (\$b == 2) {
echo "a is 1 and b is 2\n";
}
else
echo "a is 1 and b is not 2\n";
}
}
else {
if (\$b == 2) {
echo "a is not 1 and b is 2\n";
}
else
echo "a is not 1 and b is not 2\n";
}
}

to be much easier to understand than:

if(a==1 && b==2) {
echo "a is 1 and b is 2\n";
}
elseif(a==1 && b!=2) {
echo "a is 1 and b is not 2\n";
}
if(a!=1 && b==2) {
echo "a is not 1 and b is 2\n";
}
elseif(a!=1 && b!=2) {
echo "a is not 1 and b is not 2\n";
}

I find the second one needlessly complicated with unneeded tests.  Also,
the first version requires 2 tests every time.  The second version
requires 2, 4, 6 or 8 tests.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================