I came across this syntax: var == "?" - ": var. Can someone explain?
The code is as follows:
$vendors[] = array(
"id" => $row['vendorID'],
"name" => $row['name'] == "" ? "-" : $row['name'],
"tel1" => $row['phone1'] == "" ? "-" : $row['phone1'],
"tel2" => $row['phone2'] == "" ? "-" : $row['phone2'],
"mail" => $row['email'] == "" ? "-" : $row['email'],
"web" => $row['web'] == "" ? "-" : $row['web']);
Can someone explain to me what it is? See Alternate Syntax , but I could not find the information.
thank you
This is the ternary operator :
An expression
(expr1) ? (expr2) : (expr3)takes a valueexpr2if itexpr1takes a valueTRUEandexpr3if itexpr1takes a valueFALSE.
Yes, this is what others say, but it is not recommended in terms of code readability. Use it with care and do not use it without brackets around the condition.
$myvar = ($condition == TRUE) ? $valueIfTrue : $valueIfFalse;
instead
if ($condition)
{
$myvar = $valueIfTrue;
}
else
{
$myvar = $valueIfFalse;
}
This is what PHP insists on calling the "triple operator" - see http://www.phpbuilder.com/manual/language.operators.comparison.php for syntax and example.