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 Basics Daily Exercise Program Adding Logic to Your Programs

The 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

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

Thank you so much for explaining that.

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

Jason,

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.

Ya 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. ????