Output error? Or am I missing a step?


#1


https://www.codecademy.com/courses/web-beginner-en-QF3Wb/0/4?curriculum_id=5124ef4c78d510dd89003eb8


It outputs "The light is burnt out." Shouldn't it be "Turn the light on!"?


<?php
        // Write your if/elseif/else statement here!
        $lightswitch = "off";
        
        if($lightswitch = "on") {
            echo "The light is burnt out.";
        }
        
        else {
            echo "Turn the light on!";
        }
        
        
      ?>


#2

You've used an assignment operator in your if statement's comparison instead of a comparison operator.

Read more: http://php.net/manual/en/language.operators.php


#3

I see... so I should use == in place of the = after the if statement (not the defining of lightswitch). Which raises the questions of what was the code doing with the assignment operator? I read that link, but I still am uncertain about how it works behind the code in this circumstance.


#4

That returns the value that was assigned so what we have is:

if("on") ...

An empty string "" or the string "0" are considered FALSE, everything other string is considered TRUE.

So that if statement will always be TRUE.

You can read more about what is considered FALSE over here http://php.net/manual/en/language.types.boolean.php


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.