
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How do I use the ternary operator ( ? : ) in PHP as a shorthand for āif / elseā?
The syntax is as follows −
$anyVariableName=yourCondition ? if condition becomes true then this will be executed: if condition becomes false then this gets executed;
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $value=100; $result=$value >=100 ? "Greater Than or Equal to 100":"Less Than 100"; echo $result; ?> </body> </html>
Output
This will produce the following output
Greater Than or Equal to 100
- Related Articles
- If elseif else or ternary operator to compare numbers PHP?
- How can we use Python Ternary Operator Without else?
- Ternary operator ?: vs ifā¦else in C/C++
- Difference between the Ternary operator and Null coalescing operator in php
- How do I use the conditional operator in C/C++?
- How Do I Know if I Have Herpes or Something Else?
- PHP if else elseif
- What is the āif...else if...elseā statement in Java and how to use it?
- How Ternary operator in PowerShell Works?
- How do I use `SHOW COLUMNS` as a valid data source for a table?
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- What is a Ternary operator/conditional operator in C#?
- Changing ternary operator into non-ternary - JavaScript?

Advertisements