# FAQ: Booleans and Comparison Operators - Truthy and Falsy

This community-built FAQ covers the “Truthy and Falsy” exercise from the lesson “Booleans and Comparison Operators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Truthy and Falsy

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there,
As a beginner and following this course it took me a long time to figure out why in this exercise a switch wouldn’t work the same as an if/else statement. Apparently a switch does loose comparisons with “==” and gets different results than “===”, especially in this exercise. Maybe good to mention that in the part about switch statements. Now in the course it is introduced as if switch is a good substitute for “===” if/else statements.

4 Likes

They do mention the difference between them in the Identical and Not Identical Operators:

When looking through PHP code, you may encounter another operator—the equal operator (==). Like the identical operator, the equal operator will return TRUE if the left operand is the same as the right operand and FALSE if it’s not. But the equal operator is less strict than the identical operator and can have some hard to predict results, so we prefer to only use the identical operator.

If you use the equal operator, then some things might be interpreted by the computer differently to what you actually want.

For example, `5 == "5"` can come up as TRUE, where `5 === "5"` will come up as false.

Yes, thanks. But I meant specifically what type of comparison a switch statement does (==), that was not mentioned. For this exercise it makes all the difference.

2 Likes

Ah, okay, I see what you mean. Yea, a switch statement only does a loose comparison, which means it uses `==`. If you want to use a strict comparison, then its better to use an if / elseif statement.

So for this exercise, an if / elseif would have been better.

I agree that they should mention this in that part of the lesson.

Can that be the reason that my function is not accepted?

``````function truthyOrFalsy(\$str){
switch(\$str){
case '[]':
case NULL:
case '':
case '0':
case 0:
return "False";
default:
return "True"
}
}``````

Hey there, you’re overcomplicating things.

First we need to ask ourselves if a switch statement is really necessary for such a simple ask.

We only really need to check:

That if the value provided is truthy, we return true
Otherwise, we return false

How could we achieve this?

Ok, I know, I thought they want us too practice what we learned. Still my solution also should be working, but the error says, that its giving the wrong return value. Any clue why?

Your function works fine for me, you just forgot to add a semicolon after “return “True””.

Cheers

In Truthy and Falsy, i tried to use an undefined variable and it won’t accept my answer and it gives me this message instead:

PHP Notice: Undefined variable: test in /home/ccuser/workspace/php-truthy-falsy/src/index.php on line 17

note that it does print False in the console before giving me this message. I just wonder why it won’t count as a good answer for me to complete the task.

1 Like

Hey there,

What does your code look like? Could you paste it here?

undefined variable.

``````<?php

function truthyOrFalsy(\$any_value)
{
if (\$any_value) {
return "True";
}
else {
return "False";
}
}

\$test;
echo truthyOrFalsy("yay");
echo "\n";
echo truthyOrFalsy(\$test);
echo "\n";

``````

i also tried like this: undeclared variable.

``````<?php

function truthyOrFalsy(\$any_value)
{
if (\$any_value) {
return "True";
}
else {
return "False";
}
}

echo truthyOrFalsy("yay");
echo "\n";
echo truthyOrFalsy(\$test);
echo "\n";

``````

You always must declare your variables, else you’ll receive this type of error.

If you declare `\$test;`, you must assign it a value. Could be an empty string if you don’t know what it should hold, or even null.

If you don’t know whether or not a variable has been declared, you can run a quick safety check:

``````if (isset(\$test)) {
echo truthyOrFalsy(\$test);
}
``````

This will not run, because `\$test` hasn’t been declared. But you also won’t get any errors popping up.

1 Like

I understand that this message is important to let me know there is something wrong with the code and i understand why it gives me the message, my question is why codecademy doesnt accept my code when this is what they ask?

quote: Test your function! Invoke your function at least once with a truthy value and at least once with a falsy value. Be sure to use `echo` to print the results to the terminal.

it IS a falsy value since it is part of the list they gave my in the previous paragraph:

quote:

• Empty strings
• `null`
• an undefined or undeclared variable
• an empty array
• the number `0`
• the string `"0"`

They could have just give me the message, but accept my answer as it is what they are looking for (i tried all the falsy values to find out this one wasn’t accepted).

1 Like

Can anyone tell me why this code returns false?

<?php function truthyOrFalsy(\$value) { if (\$value === TRUE){ return "True"; } else { return "False"; } } echo truthyOrFalsy(1); ?>

And why does this one then return TRUE?

<?php function truthyOrFalsy(\$value) { if (\$value){ return "True"; } else { return "False"; } } echo truthyOrFalsy(1); ?>

The === checks whether its exactly the same
and `\$value` (which is `1`) is not exactly the same thing as the boolean value `TRUE`
so `(\$value === TRUE)` is `FALSE`
(=== is strict equals)

In PHP, the number 0 is falsy (meaning acts like `FALSE`), and all other numbers are truthy (meaning act like `TRUE`)
and `\$value` (is 1)
so doing `if(\$value)` is like doing `if(TRUE)`