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

If statement always evaluates to false.

I have "hand to eye" evaluated both $stringOne and the statement in the comparison and they both seem to me to be exactly the same. I must just be over looking something I am just not sure what it is. code as follows

<?php

$name = "Ray";
$stringOne = 'Learning to display ';
$stringOne .= '"Hello ';
$stringOne .= $name;
$stringOne .= '!" to the screen.';
$stringOne = $stringOne."\n";

echo $stringOne;

$isReady = true;
//var_dump($isReady);
$isReady = false;
//var_dump($isReady);

//var_dump(1 + "2)");

$a = 10;
$b = "10";

//var_dump($a == $b);
//var_dump($a === $b);

//var_dump($stringOne == 'Learning to display "Hello Ray!" to the screen.');

if($stringOne == 'Learning to display "Hello Ray!" to the screen.'){
  echo 'The values match';
} else {
  echo 'The values DO NOT match';
} 


?>

2 Answers

andren
andren
28,558 Points

You add a line break to the $stringOne variable:

$stringOne = $stringOne."\n";

Which you do not do with the string you are comparing it to, that is what makes them different. If you remove the line where you add the line break or change your if statement's condition to this:

$stringOne == 'Learning to display "Hello Ray!" to the screen.'."\n"

Then you will find that the two strings match.

@andren Thanks for the help/explanation.