Page 1 of 1

Need info on syntax

Posted: Wed Nov 05, 2003 3:00 am
by Nay

Code: Select all

ini_get('display_errors') == '1' ? 'On' : 'Off'
Hey Mac, or anyone - got a tutorial on that string's syntax? I still don't really understand it. From how i look at it, it's like if 1 is returned from ini_get('display_errors') then............*falls off*

-Nay

Posted: Wed Nov 05, 2003 5:40 am
by JAM
Also mentioned as 'Alternate Syntax' to if-then-else.
Same thing as:

Code: Select all

$var = ini_get('display_errors');
if ($var == '1') {
  echo 'On';
} else {
  echo 'Off';
}
...but as you see, much shorter.

Same idea can be used for example coloring lines in <table>'s...

Code: Select all

$i = 'red';
whie (something to loop) {
 $i = ($i == 'red' ? 'blue' : 'red'); // set $i to 'the other value'
 echo '<td bgcolor="'.$i.'">foo</td>';
}
...just to mention other usages.

Posted: Wed Nov 05, 2003 5:49 am
by Weirdan
[php_man]language.operators.comparison[/php_man]
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Posted: Wed Nov 05, 2003 6:03 am
by twigletmac
I split this from the other topic before it got too off-topic ;).

Mac

Posted: Wed Nov 05, 2003 10:40 pm
by Nay
Yay! Thanks guys...

8)

Now that's one more piece of syntax in my head :D, hope I just remember to use it when a time comes :lol:.

-Nay

Posted: Wed Nov 05, 2003 10:45 pm
by McGruff
Hate it personally, on the grounds that it's less clear. I'd recommend avoiding it but everyone's got their own style.