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.

1 thought on “Ternary Conditionals”

  1. 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 Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top