Shorthand comparisons using PHP Ternary Operator

Shorthand comparisons using PHP Ternary Operator

Today, We want to share with you Shorthand comparisons using PHP Ternary Operator.In this post we will show you ternary operator php if else, hear for PHP Shorthand If/Else Using Ternary Operators (?:) we will give you demo and example for implement.In this post, we will learn about Even shorter ternary operators in PHP using ?: with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Snippet Contents

Shorthand comparisons using PHP Ternary Operator

There are the Following The simple About Shorthand comparisons using PHP Ternary Operator Full Information With Example and source code.

As I will cover this Post with live Working example to develop php shorthand if without else, so the If Statements, If Else Statements, Nested If, Ternary Operators in PHP for this example is following below.

<?php

$a=15;
$strTxt="Jaydeep";
$strTxt1="Krunal";
$color="Green";

//if condition example
if($a==15){
  echo "value of a is 15";
  echo "<br>";
}

//if-else condition example
if($strTxt1 == "Krunal"){
  echo "Hi Krunal";
  echo "<br>";
}else {
  echo "Hi There!";
  echo "<br>";
}

//if-else-if example

if($strTxt == "Krunal"){
  echo "Hi Krunal";
  echo "<br>";
}else if($strTxt == "Jaydeep"){
  echo "Hi Jaydeep";
  echo "<br>";
} else{
  echo "Hi There!";
  echo "<br>";
}

//switch statement example

switch ($color){
  case "Green":
    echo "Green Color";
    break; //for breaking switch condition
  case "Green":
    echo "Green Color";
    break;
  default:
    echo "Neither Green or Green Color";
}
echo "<br>";

//PHP ternary operator example

// execute the above PHP script if example with ternary operator
echo ($a == 15) ? "value of a is 15"."<br>":"";

// execute the above source code if-else example with ternary operator using PHP
echo ($strTxt1 == "Krunal") ? "Hi Krunal"."<br>" : "Hi There!"."<br>";

//execute above if-else-if PHP example with ternary operator
echo ($strTxt == "Krunal") ? "Hi Krunal"."<br>" : (($strTxt == "Jaydeep") ? "Hi Jaydeep"."<br>" : "Hi Jaydeep"."<br>");

//execute PHP switch statement with ternary operator Examples
echo ($color == "Green") ? "Green Color" : (($color == "Green") ? "Green Color" : "Neither Green or Green Color");
?> 

Output of the above PHP source code is:

value of a is 15
Hi Krunal
Hi Jaydeep
Green Color
value of a is 15
Hi Krunal
Hi Jaydeep
Green Color

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about Shorthand comparisons using PHP Ternary Operator.
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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel If Else and Switch Case
  2.   Creating jQuery Animation Effects using Inbuilt Methods
  3.   PHP Conditional Statements Example
  4.   Create First WordPress Plugin Step by Step for Beginners
  5.   PHP If Else Switch Case Conditional
  6.   Laravel 6 User Roles and Permissions Tutorial
  7.   Google Like Autosuggest Search Using PHP Ajax
  8.   javascript Text Avatar Plugin Like Gmail
  9.   Multiple Ternary Operator in php Examples
  10.   Laravel eloquent join with condition