Ternary Conditionals

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.

One comment

  • 1
    December 4, 2008 - 2:20 am | Permalink

    I’m 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

  • Leave a Reply

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

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">