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 trialcasthrademosthene
6,275 PointsThe statement is always correct, no matter how I change it. HELP
$string_one = 'Learning to display';
if ($string_one = 'Learning to display to the screen.') { echo 'the values matcch '; } else { echo 'the values DO NOT match'; }
RESULTS:
treehouse:~/workspace$ php strings.php
the values matcch treehouse:~/workspace$
2 Answers
Christian Andersson
8,712 PointsYou forgot the second equal-sign in the evaluation. Change your code from:
if ($string_one = 'Learning to display to the screen.')
to
if ($string_one == 'Learning to display to the screen.')
Wondering why it was always true before? It is because you were assigning $string_one
the value of "Learning to display to the screen
". The operation of assigning that value to the variable is successful, so that gives a true
which enters the if
.
casthrademosthene
6,275 PointsYa Christian's explanation was easy to understand and as a complete newbie in the industry and php it was the perfect answer for me. So once again thanks Christian and also thanks Jason for the additional output. ????
casthrademosthene
6,275 Pointscasthrademosthene
6,275 PointsThank you so much for explaining that.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Christian,
An assignment expression will evaluate to whatever value is being assigned. In this case it evaluates to the string 'Learning to display to the screen.'
That will then be tested as a boolean which will be
true
If on the other hand, the condition was:
if ($string_one = "0")
then that will always be false even though the assignment is successful. The assignment operation will evaluate to the string "0" which will evaluate to false when tested as a boolean.
Christian Andersson
8,712 PointsChristian Andersson
8,712 PointsJason,
You're absolutely right, my explanation was quite generalized, but let's not beat around the bush here - this is mostly a beginners forum and I think the simpler and more memorable explanations are more applicable here, even if they are not always 100% accurate. The complex ones probably belong to communities such as Stackoverflow, unless OP specifically seeks such answer.