PHP: Nested if statements?

Caporegime
Joined
18 Oct 2002
Posts
25,287
Location
Lake District
I know how to do nested if statements using the curly brackets but I can't figure out how to do it with the shorthand if's.

eg

<?= (($blah = 1)? 'do this' : 'do that'); ?>

how would I nest another if in the do this bit?
 
I wouldn't sue nested ternary if statements - they tend to get very unreadble, very quickly.

If you must though:

Code:
<?= (($blah = 1) ? (($blah2 = 1) ? 'do this' : 'or this') : 'do that'); ?>
 
Last edited:
fyi, they are called ternary operators. :)

Code:
($blah == 1)
  ? 'do this'
  : ($yada == 1)
    ? 'do that'
    : 'do something else'
 
you would put the same thing in the true/false partitions. However, why are you doing this? The code would be very hard to read - they are called ternary IFs for a reason ;)


<?= ($blah = 1) ? '( ($something = 1) 'do this' : 'do that' ) ' : 'do that'; ?>
 
Back
Top Bottom