Linchakin

20th Sep - PHP ternary operator

 September 20, 2021     No comments   

In this article, we will understand about a PHP ternary operator with the help of various examples.

What do you mean by Ternary operator?

It is also called a conditional operator. The ternary operator is an operator that decreases the length of code while performing comparisons. It is an alternative for using if-else and nested if-else statements. We can use the ternary operator when there is a need to simplify the if-else statements and to make efficient code out of a complex program structure.

PHP ternary operator

Syntax:

PHP ternary operator

Following is the syntax of ternary operator in php.

In the above syntax,

  • Condition statement: It is a PHP expression that will be evaluated in order to return a Boolean value.
  • code1: This will be the statement that will be executed when the conditional results will return true.
  • code2: This will be the statement that will be executed when the conditional results will return in a false state.

Flow diagram of PHP:

PHP ternary operator

Example 1:

Explanation:

In the above example, we have taken a condition that if the condition is true then the 20 value is displayed otherwise the 5 value is displayed.

Output:

Following is the output of this example:

PHP ternary operator

Example 2:

Explanation:

In the above example, we have taken a condition that if the condition is true then the passed message is displayed otherwise the fail message is displayed.

Output:

Following is the output of this example:

PHP ternary operator

Example 3:

Explanation:

In the above example, we have taken a condition that if the age is greater, the adult message is displayed otherwise the not adult message is displayed.

Output:

Following is the output of this example:

PHP ternary operator

Example 4:

Explanation:

In the above example, we have taken a condition that if the condition is true, the eligible for credit card message is displayed otherwise the Not eligible for credit card is displayed.

Output:

Following is the output of this example:

PHP ternary operator
Next Topic#

Adblock test (Why?)


You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss

Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Moonwalk - Cover Your Tracks During Linux Exploitation By Leaving Zero Traces On System Logs And Filesystem Timestamps Introduction moonwalk is a 400 KB single-binary executable that can clear your traces while penetration testing a Unix machine. It saves the state o… Read More
  • MakeMusic Finale 27 Crack incl Activator Key 2022 Mac (Full Version) Table of Contents MakeMusic Finale 27 Crack with Activation Code 2022 Latest Version Free Download MakeMusic Finale 27 Crack simple to use and com… Read More
  • BackupOperatorToDA - From An Account Member Of The Group Backup Operators To Domain Admin Without RDP Or WinRM On The Domain Controller If you compromise an account member of the group Backup Operators you can become the Domain Admin without RDP or WinRM on the Domain Controller. All … Read More
  • Blockchain Pokémon and Monetized NFTs: 5 Tokens Enabling VR in the Metaverse The Sandbox, Decentraland, and RFOX VALT use VR in their immersive playgrounds. Users can enter large retail SHOPs represented by an NFT to purchase … Read More
  • Package Analysis – OpenSSf Tool to Detect Malicious Packages in Popular Open-Source Repositories A prototype version of the Package Analysis tool has been recently released by the Open Source Security Foundation (OpenSSF), and it is the first of … Read More
Newer Post Older Post Home

0 Comments:

Post a Comment


Copyright © 2025 Linchakin | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates