What is stored in $bonus after this code is processed (PHP programing) ?

Hello

New member
If a variable named $carsSold contains the value 8, What is stored in $bonus after this code is processed?

if ($carsSold < 10)
$bonus = 0.00;

if ($carsSold < 20)
$bonus = 100.00;

else
$bonus = 200.00;

a. 0.00
b. 100.00
c. 200.00

which one is the answer? why? can someone please explain this to me? thank!!
 
This is a very simple example of what is known as "code tracing."

Trace it, following the logic. First, $carsSold = 8.
It checks the first conditional, and since 8 < 10, $bonus = 0.00;
It checks the second conditional, and since 8 is also <20, $bonus = 100.00;

The else block only applies to the second conditional, so it's ignored entirely.

The answer is b) 100.00.
 
Back
Top