I just found the following code in a PHP script and was wondering why it didn’t cause PHP to report an error?
$current_name == ($type != 3) ? $name : '' ;
It was a typo and the code was supposed to read:
$current_name = ($type != 3) ? $name : '' ;
That is a bizarre bit of code, but only because it is unreadable and useless, not because it is invalid. It uses the ternary operator, which is basically a shorthand
ifconstruct in the formatcondition ? if true : if false.This code does the following:
$type != 3. If$typeis3, returnfalse, otherwisetrue.$current_name.true(i.e.$current_name == true), return$name. Otherwise (i.e.$current_name == false) return''.Of course, all this does absolutely nothing, because there is no assignment in the statement.