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

Oleg Kuperman
Oleg Kuperman
2,188 Points

Why? please explain in a bit of detail

$a = " "; if ($a = 5) { echo "Hello ok, "; } echo "Welcome home!";

In this IF statement, why is the answer: Hello ok, Welcome home!

When $a = 5

1 Answer

Zachary Billingsley
Zachary Billingsley
6,187 Points

Hello!

It looks like you are using an assignment operator in your IF statement.

But you don't want to assign in your IF statement, you want to compare.

The single '=', will assign a value to a variable.

The double '==', will compare values. So try using the '==' in your IF statement.

Hope that helps!

Oleg Kuperman
Oleg Kuperman
2,188 Points

so the first =, assigned the variable " " (null), then in the IF statement, did that variable then become assigned the integer 5, for the answer to come out that way?

Zachary Billingsley
Zachary Billingsley
6,187 Points

Correct, the code within the condition of the IF statement reassigned the value of $a to 5.

So the computer sees IF ( 5 ) { echo "Hello ok, "; }, and 5 is a TRUE value in PHP.