I know most of you allready use them, but there are still people that don`t know about them, so to make the following tutorials easyer, I`ll do a small example.

First of all you must know that all you will see here is a simpler way to write not so complex if statements.
Traditional way:

if (condition) {
    echo "true";
} else {
    echo "false";
}

The shorter way:

echo (condition) ? "true"  : "false";

Another example:

if (date("m")<12) {
    $var = 12-date("m") ." months till december";
}else{
    $var = "It`s December";
}
echo $var;

The shorter way:

$var = (date("m")<12) ? 12-date("m")." months till december" : "It`s December";
echo $var;

I hope you got it and this will make easyer for you to undersand the following tutorials.

Ternary Conditionals
Tagged on:     

One thought on “Ternary Conditionals

  • December 4, 2008 at 2:20 am
    Permalink

    Im a teacher of entrepreneurship in our school, and I think this document of yours can be of help to me and my students to become successful someday. Thanks a lot. god blezzz

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *