Need info on syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Need info on syntax

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I split this from the other topic before it got too off-topic ;).

Mac
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

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