How to use Conditional Operator in PHP
In this article, I will explain how the Conditional operator can be used in PHP.
PHP Operators
PHP language supports following type of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Conditional Operators
The Conditional operators are summarized in the following table.
Operator |
Description |
Example |
|
? : |
Conditional Expression |
If Condition is true ? Then value X : Otherwise value Y |
|
Example
<html>
<head><title>condition Operators</title><head>
<body>
<?php
$x = 20;
$y = 30;
$result = ($x > $y ) ? $x :$y;
echo "Test Result1 : Value of result is $result<br/>";
$result = ($x < $y ) ? $x :$y;
echo "Test Result2 : Value of result is $result<br/>";
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here