another good example of using ternary operator is
<?php
$a = 20;
$b = 30;
$c = 70;
echo ($a > $b && $a > $c) ? $a : ($b > $a && $b > $c) ? $b : $c;
?>
![]() |
|
||||||||||
|
else
Often you'd want to execute a statement if a certain condition is
met, and a different statement if the condition is not met. This
is what else is for. else
extends an if statement to execute a statement
in case the expression in the if statement
evaluates to FALSE. For example, the following
code would display a is bigger than
b if else
ravi dot goglobium at gmail dot com
05-Sep-2007 09:49
another good example of using ternary operator is
mitch at mitchellbeaumont dot com
24-Jul-2007 12:09
At gwmpro at yahoo dot com
robbak
21-Jun-2007 12:54
Yes, that code is clearly ambiguous. I would think that the code does the 'right thing' with it anyway. The else should bind to the nearest if.
jsimlo
15-Aug-2006 07:30
This generates a parser error:
gwmpro at yahoo dot com
04-May-2006 08:00
I am new to this language. It seems to me that only the semicolon ';' is required, the brackets '{}' are not if there is only one statement. The code segment below would be legal.
Caliban Darklock
08-Nov-2004 11:24
If you're coming from another language that does not have the "elseif" construct (e.g. C++), it's important to recognise that "else if" is a nested language construct and "elseif" is a linear language construct; they may be compared in performance to a recursive loop as opposed to an iterative loop.
cap at capsi dot com
05-Oct-2000 10:58
Often you can avoid large if/else statements in your code by using the ternary operator. For example: |