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 trialOleg Kuperman
2,188 PointsWhy? 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
6,187 PointsHello!
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
2,188 PointsOleg Kuperman
2,188 Pointsso 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
6,187 PointsZachary Billingsley
6,187 PointsCorrect, 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.
Oleg Kuperman
2,188 PointsOleg Kuperman
2,188 PointsThank you!