ternary operator in php

Solutions on MaxInterview for ternary operator in php by the best coders in the world

showing results for - "ternary operator in php"
Clarence
21 Jan 2017
1<?php 
2#Syntex  
3(if Condition) ? (stat1) : (stat2);
4
5#example
6$var1 = 5;
7$var2 = 2;
8
9echo $check = ($var1 > $var2) ? "right" : "wrong";
10
11#output : right
12/*
13explination : if condition is true then display the stat1 and if condition is 
14worng then display stat2
15*/
16?>
Karl
12 Jun 2019
1(Condition) ? (Statement1) : (Statement2);
2
Irene
10 Jul 2016
1<?php
2$marks=40;
3print ($marks>=40) ? "pass" : "Fail";
4?>
Farley
17 Jan 2021
1// Both ternary and if/else returns the same result
2
3// ternary
4$result = $condition ? 'foo' : 'bar';
5
6// if/else
7if ($condition) {
8    $result = 'foo' 
9} else {
10    $result = 'bar'
11}
Nick
12 Jun 2020
1echo $color = $color ?? 'red';	//if value not exists then assign to them.
Claudio
08 Mar 2017
1<?php
2  $x=4;
3  $y=3;
4  if(x>y)?echo"X is large": echo "y is large";
5
6 ?>
7