Home Articles/Tutorials Login Register
WebCodeLogic

PHP Ternary Operator ? :

 ← Dev Articles
👍 0
👎 0

$val = 10;
$total = 0;
$total = $val > 9 ? ($val + 10) : 0;

echo $total; // $val equals 20

Here is another example:

   $newValue = $val ?: 9;

In this example $newValue is 10.  Notice when $val is NOT null the $newVal will be the value of $val otherwise it's the value behind ?: which is 9 in this case.

 PHP +21  Number of articles in category PHP
 Laravel +14  Number of articles in category Laravel
 Javascript +14  Number of articles in category Javascript
 VueJS +6  Number of articles in category VueJS
 NodeJS +6  Number of articles in category NodeJS
 LAMP +5  Number of articles in category LAMP
 PostgreSQL +3  Number of articles in category PostgreSQL
 MySQL +3  Number of articles in category MySQL
 MongoDB +2  Number of articles in category MongoDB
 ImageMagick +2  Number of articles in category ImageMagick
 Database +1  Number of articles in category Database
 ReactJS +1  Number of articles in category ReactJS
 Composer +1  Number of articles in category Composer
 Bootstrap +1  Number of articles in category Bootstrap
 Blade +1  Number of articles in category Blade
  •  Most Popular

  • Use PHP Recursion to Calculate the Sum of Multi-dimensional Arrays

    AsyncNinja

     more..
  • PHP Ternary Operator ? :

    PixelCompiler

     more..
  • Upload and Resize an Image with Laravel Vue and ImageMagick

    CodeCascade

     more..
  •  Most Recent

  • JavaScript Promises and Asynchronous Handling Explained

    CodeCascade

     more..
  • JavaScript Asynchronous Programming with Promise any()

    CodeCascade

     more..
  • Master JavaScript Asynchronous Programming with Promise.allSettled()

    CodeCascade

     more..
WebCodeLogic