PHP Ternary shorthand If/Else Examples

Today, We want to share with you PHP Ternary shorthand If/Else.In this post we will show you ternary operator in php for three variables, hear for multiple ternary operator in php we will give you demo and example for implement.In this post, we will learn about PHP Shorthand If/Else Using Ternary Operators (?:) with an example.

PHP Ternary shorthand If/Else

There are the Following The simple About PHP Ternary shorthand If/Else Full Information With Example and source code.

As I will cover this Post with live Working example to develop php ternary without else, so the ternary operator php 7 for this example is following below.

Also Read :  Convert Array To Object With PHP
PHP Ternary shorthand If-Else Example
PHP Ternary shorthand If-Else Example

PHP Ternary shorthand Examples

index.php

<?php
$valid = true;
$lang = 'french';


if( $valid ) {
    $x = 'yes';
} else {
    $x = 'no';
}

echo "using If else Conditions:".$x;
echo "<br/>";

$x = $valid ? 'yes' : 'no';
echo "using ternary Conditions:".$x;
echo "<br/>";


$x = $valid ? ($lang === 'french' ? 'oui' : 'yes') : ($lang === 'french' ? 'non' : 'no');
echo "using Multiple ternary Conditions:".$x; // outputs 'oui'
echo "<br/>";


/* Ternary shorthand */

$counter = "";

if( $counter ) {
    $counter = $counter;
} else {
    $counter = 1;
}

echo "Way 1: using if else ternary Conditions:".$counter;
echo "<br/>";


if( !$counter ) {
    $counter = 1;
}

echo "Way 2: using shorthand ternary Conditions:".$counter; 
echo "<br/>";

$counter = $counter ?: 1;
echo "Way 3: using Best way to ternary Conditions:".$counter; 
echo "<br/>";

?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Also Read :  How To Import CSV File into MySQL using PHP

I hope you get an idea about PHP Ternary shorthand If/Else.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.