1

The ternary operations in PHP

When we heard the word “ternary”, we can link this word with different thinks and more if we do not pretty well the english lenguage, but don´t worry about because I will explain you what is this and what is its meaning.

It is important to keep in mind that in PHP as well as in other programming lenguages there are different kind of operations:

  • Unary: Operations that only use one digit, for example ++1, --2, etc.
  • Binary: The common operation. 2 + 2, 5 - 2, etc.
  • Ternary: The operations that involve three values.

The last one is the topic of this article and I will give a precise example of this kind of operation that exists not only in PHP, but in many other lenguages. Although it is important to mention that the way to use this kind of operation varies in each programming lenguage.

So, the following explains better the way of this type of operation:


                <span class="hljs-type">This</span><span class="hljs-keyword">is</span> a sample

                10;
                        $result = ($x > 5) ? "$x is greater than 5" : "5 is greater than $x";
                        print $result;
                ?>

it is important to keep in mind that I am using the php script embedded in a html file because I am not using a large php script yet. The result of this script is the following

<html><head><title>This isa sampletitle>
        head>
        <body>10is greater than 5
        body>
html>

I hope this had would been helpful for you

Escribe tu comentario
+ 2