Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

76 users online



Daily Snippet 9-18-06

Daily Snippet 9-18-06

Currently viewing this thread: 1 (0 members and 1 guests)


erod

erod

uh yea
Status: Offline!

Daily Snippet 9-18-06

Ternary conditions are a great way to condense code and tidy it.

PHP:

<?php

if(isset($_GET['foo']))
{
    
$var "foo";
}
 else
{
    
$var "bar";
}

?>

Alternatively:

PHP:

<?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:

<?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.

James

James

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!

Xolt

Xolt

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.

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0071 seconds.