- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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;
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
Advertisements