I'd like to to some thing similar to JavaScript's
var foo = true; foo && doSometing();
but this doesn't seem to work in PHP.
I'm trying to add a class to a label if a condition is met and I'd prefer to keep the embedded PHP down to a minimum for the sake of readability.
So far I've got:
<?php $redText='redtext ';?> <label class="<?php if ($requestVars->_name=='')echo $redText;?>labellong">_name*</label> <input name="_name" value="<?php echo $requestVars->_name; ?>"/>
but even then the IDE is complaining that I have an if statement without braces.
In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.
The term "ternary operator" refers to an operator that operates on three operands. An operand is a concept that refers to the parts of an expression that it needs. The ternary operator in PHP is the only one that needs three operands: a condition, a true result, and a false result.
In PHP, there are 4 different types of Conditional Statements.
use the ternary operator ?:
change this
<?php if ($requestVars->_name == '') echo $redText; ?>
with
<?php echo ($requestVars->_name == '') ? $redText : ''; ?>
In short
// (Condition)?(thing's to do if condition true):(thing's to do if condition false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With