You are here: Re: if...else - in which order « PHP Programming Language « IT news, forums, messages
Re: if...else - in which order

Posted by Jerry Stuckle on 11/04/06 02:36

David Gillen wrote:
> An noise sounding like Sonnich said:
>
>>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";
>>
>
> Simplify your code by writing more, but it will help you.
> 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.

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация