(Condition) ? (Statement1) : (Statement2);
<?php
$marks=40;
print ($marks>=40) ? "pass" : "Fail";
?>
$result = $condition ? 'foo' : 'bar';
// Both ternary and if/else returns the same result
// ternary
$result = $condition ? 'foo' : 'bar';
// if/else
if ($condition) {
$result = 'foo'
} else {
$result = 'bar'
}
$foo = your_value;
$bar = ($foo == 1) ? "1" : (($foo == 2) ? "2" : "other");
echo $bar;
$y = $x ? "true" : "false";
echo $color = $color ?? 'red'; //if value not exists then assign to them.
(conditional) ? (execute this when true) : (execute this when false);
# example
<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Not Adult";
?>
# output
Adult
print ($marks>=40) ? "pass" : "Fail";
//Use this format before reducing the expression to one liner
$var=4; //Change value to test
echo "Format result: ";
echo($var === 1) ? 'one' : //if NB.=> $varname = || echo || print || var_dump(ternary statement inside); can only be (placed at the start/wrapping) of the statement.
(($var === 2) ? 'two' : //elseif
(($var === 3) ? 'three' : //elseif
(($var === 4) ? 'four' : //elseif
'false' //else
))); //extra tip: closing brackets = totalnumber of conditions - 1
// Then echo($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false')));
echo "<br/>";
var_dump("Short result: ", ($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))) );
<?php
$x=4;
$y=3;
if(x>y)?echo"X is large": echo "y is large";
?>
$if = function($test, $true, $false)
{
return $test ? $true : $false;
};
echo "class='{$if(true, 'abc', 'def')}'";
$customer->user->fullName ?? ''
$customer->user->fullName ? $customer->user->fullName : ''
isset($customer->user->fullName) ? $customer->user->fullName : ''
<?php
$is_user_logged_in = false;
$title = $is_user_logged_in ? 'Logout' : 'Login';Code language: HTML, XML (xml)
Code Example |
---|
Php :: php upload from url |
Php :: delete image s3 laravel |
Php :: laravel seed |
Php :: php json encode |
Php :: show created_at as normal date laravel blade |
Php :: append file in php |
Php :: laravel find by |
Php :: laravel model is dirty |
Php :: how to get the number of days in the current month using carbon |
Php :: time to string in php |
Php :: php abs() |
Php :: php get highest key value |
Php :: laravel eloquent where id not equal to |
Php :: return back in blade laravel |
Php :: how to backup laravel project |
Php :: https redirect in htacess for php laravel |
Php :: codeigniter redirect |
Php :: get words after string in in php |
Php :: php read csv to array |
Php :: laravel foreach first |
Php :: install laravel |
Php :: phpcs |
Php :: php const |
Php :: percentage in php |
Php :: mac os down upgrade php |
Php :: show random post in laravel |
Php :: php random number generator |
Php :: file put contents append |
Php :: laravel asset |
Php :: file storage laravel |