kennethhope
0
Q:

php ternary operator

<?php 
#Syntex  
(if Condition) ? (stat1) : (stat2);

#example
$var1 = 5;
$var2 = 2;

echo $check = ($var1 > $var2) ? "right" : "wrong";

#output : right
/*
explination : if condition is true then display the stat1 and if condition is 
worng then display stat2
*/
?>
4
(Condition) ? (Statement1) : (Statement2);
5
<?php
$marks=40;
print ($marks>=40) ? "pass" : "Fail";
?>
2
$result = $condition ? 'foo' : 'bar';
1
print ($marks>=40) ? "pass" : "Fail";
0
<?php 
$name = isset($_POST['Name'])?$_POST['Name']:null;  
$age = isset($_POST['Age'])?$_POST['Age']:null;  
?> 
0
<?php 
if(isset($_POST['Name'])) 
    $name = $_POST['Name']; 
else
    $name = null; 
  
if(isset($_POST['Age'])) 
    $age = $_POST['Age']; 
else
    $age = null; 
?> 
0
<?php 
$age = 20; 
print ($age >= 18) ? "Adult" : "Not Adult"; 
?> 
0
<?php 
$a = 10; 
$b = $a > 15 ? 20 : 5; 
print ("Value of b is " . $b); 
?> 
0
www.edureka.co
0

New to Communities?

Join the community