Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

elseif short statement



11:59 pm on Dec 14, 2010 (gmt 0)


dos anyone know if there is a short else if statement in PHP?

usually this :
echo ($Condition)?"Condition is True" : "Condition is False";

is the equivalent of :

if ($Condition) {
echo "Condition is True";
echo "Condition is False";

is there any short scrip for this ?

if ($Condition)

echo"Condition is True" ;

elseif ($Condition)

echo"Condition is Whatever...";

echo "Condition is False";


8:51 am on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Hi there TheKiller,

I suppose that depending on the context you could have a nested ternary, could be a bit tricky, depending on what would be needed, but once the logic is sorted out I would think that an appropriately nested ternary would do the job!

They are handy things to learn especially if you want to try and covert a paragraph of code into a more digestible statement.



4:41 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You **can** "stack" ternary statements but as noted in the documentation [php.net], you have to be cautious. The example there doesn't prove the case for me, but shows the potential for confusion.


4:56 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member


Agreed, though they can be confusing, they are also pretty useful. Sometimes having a paragraph of well structured code negates the need to condense if for 'elegance' sake - if it ain't broke, don't fix it.

Nested ternarys are just a preference of mine. I use them whenever I feel as they are the better choice in a logical way..



1:16 am on Dec 19, 2010 (gmt 0)

Hi Matt
i have made up with this yesterday ..

echo ($Pass == "0")?"Does not require password!" ($Pass == "1")?"Password is needed!":"N/A";

but it is giving me this error ..

Parse error: syntax error, unexpected '(' in C:\wamp\www\UT\index.php on line 223

im going to have a look at that link that rocknbil gave and see if i can come up with something :)


I Have looked around this reply :
With Nested ternary Operators you have to set the logical parentheses to get the correct result.


($test) ? "TEST1 true" : ($test2) ? "TEST2 true" : "false";
This will output: TEST2 true;



($test) ? "TEST1 true" : (($test2) ? "TEST2 true" : "false");

Anyway don't nest them to much....!

i came out with this two lines but they echo nothing :-?

($Pass == "0") ? "Does not require password!" : ($Pass == "1") ? "Password is needed!" : "N/A";

($Pass == "0") ? "Does not require password!" : (($Pass == "1") ? "Password is needed!" : "N/A");


2:00 pm on Dec 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

here you go,

$message= $Pass==0? "Does not Require Password": ($Pass==1 ? "Password is needed!" : "N/A");
echo $message;

Pass=0, message is "Does not Require Password"
Pass=1, message is "Password is needed!"
and for any other value of Pass it will say N/A


8:49 pm on Dec 19, 2010 (gmt 0)

Thank You Anyango !

That Works Perfect :D

Featured Threads

Hot Threads This Week

Hot Threads This Month