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:

Learn PHP

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (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

Learn more about how to use this guide.

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.

3 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