
September 19th, 2006
01:27 AM
Daily Snippet 9-18-06
Ternary conditions are a great way to condense code and tidy it.
<?php
if(isset($_GET['foo']))
{
$var = "foo";
}
else
{
$var = "bar";
}
?>
Alternatively:
<?php
$var = (isset($_GET['foo'])) ? "foo" : "bar";
?>
Code condensing with ternary conditions are fun, and can get very advanced, nesting multiple conditions is very easy;
<?php
$var = (isset($_GET['foo'])) ? "foo" : ((isset($_GET['bar'])) ? "bar" : "no food and no bar");
?>
Tomorrow:
Some apache talk about htaccess and the lovely mod rewrite
___________________
I code alot.

September 19th, 2006
03:28 AM
Development Forum Leader
Status: Offline!
Ternary conditions are ok for a simple if-else statement but more complex statements aren't desirable because they are hard to read.
erod - Good idea with the daily snippet, perhaps you can title them more appropriately though.
___________________
irc.efnet.net #neverside
Neverside merchandise!

September 28th, 2006
02:32 PM
Neverside Newbie
Status: Offline!
If I remember correctly, ternary conditions have clocked faster than if/else statements, but using them in more complex statements, as James said, can not only get tricky to the eye, but could possibly slow down script processing just a wee bit.