php one line if else code example

Example 1: php inline if

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

Example 2: if online php

<?php echo ($requestVars->_name == '') ? $redText : ''; ?>

Example 3: write if and else in one line php

$result = ($data->status == 1) ? 'active' : 'disable'

Example 4: one line if statement php

echo ($var1 >= $var2) ? 'this is true' : 'this is false';

Example 5: one lin if statement php

<?php if ($requestVars->_name == '') echo $redText; ?>

Example 6: php one line if without else

$thisVar != $thatVar ?: doThis();
or
($thisVar == $thatVar) && doThis();
or
if ($thisVar == $thatVar) doThis();

Tags:

Php Example