homepage Welcome to WebmasterWorld Guest from 54.211.138.180
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
elseif short statement
TheKiller




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

Hello

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";
}
else{
echo "Condition is False";
}



is there any short scrip for this ?

if ($Condition)

echo"Condition is True" ;

elseif ($Condition)

echo"Condition is Whatever...";
else

echo "Condition is False";

 

Matthew1980




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

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.

Cheers,
MRb

rocknbil




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

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.

Matthew1980




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

^^^

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

Cheers,
MRb

TheKiller




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


EDIT :

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

<?php
$test=true;
$test2=true;

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

correct:

<?php
$test=true;
$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");

Anyango




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

here you go,

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

For
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

TheKiller




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

Thank You Anyango !

That Works Perfect :D

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved