- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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;
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>
This will produce the following output
Greater Than or Equal to 100
- 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++
- PHP if else elseif
- How do I use the conditional operator in C/C++?
- Difference between the Ternary operator and Null coalescing operator in php
- How do I use `SHOW COLUMNS` as a valid data source for a table?
- How do I use tabHost for Android?
- What is the ‘if...else if...else’ statement in Java and how to use it?
- Ternary Operator in C#
- Ternary Operator in Java
- Ternary Operator in Python?
- How do I overload the  operator in C#?
- How Ternary operator in PowerShell Works?