Folks,
Experimented like this about a month ago.
Look at the various ternary operator you can write:
1.
[code]
<?php
//Example 1
$i = 1;
//if(condition ? THEN : ELSE).
if($i == 1 ? $i++ : 1);
echo $i; echo ‘<br>’;
?>
2.
[code]
<?php
//Example 2
$i = 1;
//condition ? THEN : ELSE.
$i == 1 ? $i++ : 1;
echo $i; echo ‘<br>’;
?>
3.
[code]
<?php
//Example 3
$i = 1;
/*if(condition ?
THEN :
ELSE);
*/
if($i != 1 ?
$i=1 :
$i++);
echo $i; echo ‘<br>’;
?>
4.
[code]
<?php
//Example 4
$i = 1;
/*condition ?
THEN :
ELSE.
*/
$i != 1 ?
$i=1 :
$i++;
echo $i; echo ‘<br>’;
?>
5.
[code]
<?php
//Example 5
$i = 1;
/*if(condition
? THEN
: ELSE).
*/
if($i != 1
? $i=1
: $i++);
echo $i; echo ‘<br>’;
?>
6.
[code]
<?php
//Example 6
$i = 1;
/*condition
? THEN
: ELSE.
*/
$i != 1
? $i=1
: $i++;
echo $i; echo ‘<br>’;
?>
7.
[code]
<?php
$default = 0;
//(Do this if result is TRUE ? : ELSE).
if($default++ ? : 1);
echo $default; echo ‘<br>’;
?>
8.
[code]
<?php
$default = 0;
//Do this if result is TRUE ? : ELSE.
$default++ ? : 1;
echo $default; echo ‘<br>’;
?>
9.
[code]
<?php
$default = 1;
//(Do this if result is TRUE ? : ELSE).
if($default++ ? : 1);
echo $default; echo ‘<br>’;
?>
10.
[code]
<?php
$default = 1;
//Do this if result is TRUE ? : ELSE.
$default++ ? : 1;
echo $default; echo ‘<br>’;
?>
Do you know of any more ways of shortcutting the ternary operator ?