I am getting a strange result from using a ternary oparator to change the value of an integer to a grouped string.
In javascript this code would work as the ternary operator would short circuit when it get's the first true.
My code for some reason is giving a return value of 'B'
most of the time, but can print 'A'
. Is there any way to return when the first match occurs, or do I have to change the structure if to if/else
conditions.
function graderator($grade) {
return (
$grade < 65 ? 'F' :
$grade < 70 ? 'D' :
$grade < 80 ? 'C' :
$grade < 90 ? 'B' : 'A'
);
};
var_dump(graderator(58)); // => 'B' should be 'F'
var_dump(graderator(68)); // => 'B' should be 'D'
var_dump(graderator(78)); // => 'B' should be 'C'
var_dump(graderator(88)); // => 'B' should be 'B'
var_dump(graderator(98)); // => 'A' should be 'A'
Cheers
You forgot the parenthesis