Adamski Adamski - 4 months ago 16
PHP Question

php operators , this example has confused me

The code below:

$currentTime = 10;
$currentDayText = 'Fri';
$roomid = 25;

var_dump($currentTime > 6); //true
var_dump($currentDayText == 'Fri'); //true
var_dump($roomid == 25); //true
var_dump($roomid == 25 && $currentDaytext == 'Fri' && $currentTime > 6); //false?


The example above has 3 declared variables, each one when wrapped in the method var_dump() relates to true. However if I group all 3 in one condition it evaluates to false. Could somebody kindly explain why so I can understand why this happens?

Thanks in advance

Answer

$currentDaytext is undefined, so it's implicitly false. The variable you defined and assigned true to is $currentDayText - notice the capital T.

If you fix that mistake, applying && to three expressions that evaluate as true will indeed evaluated to true too.

Comments