Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Conditionals Compare

Francois-Victor LOUVEL
Francois-Victor LOUVEL
1,405 Points

Would'nt "if ($a = "Treehouse") {}" return an error ?

One of the question is erroneous, I think... an if statement written as such would return an error, no ?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! No, it wouldn't. It is possible to check an assignment inside of an if statement. The boolean returned will be based on the "truthiness" of the value assigned. In this case, true would be the evaluation. However, if we had done:

if($a = 0) {}

Then the evaluation would be false. Because 0 is a "falsey" value in PHP. You can find the documentation here.

I once read a great post on this topic, but I'm unable to locate it now. However, I found this on Stackoverflow.

Hope this helps! :sparkles:

Francois-Victor LOUVEL
Francois-Victor LOUVEL
1,405 Points

Thank you for you reply and the added links !