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

Shoaib Khan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Shoaib Khan
Front End Web Development Techdegree Graduate 21,177 Points

Cannot get strings to match in if statement.

I am having a hard time getting my strings to match in my if statement. What am I missing?

<?php
$name = 'Alena';
$string_one = "Learning to display \"Hello $name!\" to the screen.\n";
$string_one = 'Learning to display ';
$string_one .= '"Hello ';
$string_one .= $name;
$string_one .= '!" to the screen.';
$string_one = $string_one . "\n";
//prepend to a string
$string_one = 'I am ' . $string_one;
echo $string_one;

$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($string_one);

//($string_one == 'Learning to display "Hello Alena!" to the screen.');
if ($string_one == 'I am Learning to display "Hello Alena!" to the screen.') {
  echo 'the values match';
  } elseif ($string_one == "") {
    echo '$string_one is empty';
} else {
  echo 'the values DO NOT match';
}

?>

Thanks,

Shoaib Kamal Khan

2 Answers

Steven Parker
Steven Parker
229,608 Points

On line 8, a newline character is added to $string_one. But the string it is being matched with on line 29 does not contain a newline character, so they truly do not match.

Either add a newline on line 29, or remove line 8, either will allow them to match.

Shoaib Khan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Shoaib Khan
Front End Web Development Techdegree Graduate 21,177 Points

Hi Steven,

I have added a new line on line 29, but I am having the same problem.

Here is the code that I have:

if ($string_one == 'I am Learning to display "Hello Alena!" to the screen.\n') {
  echo 'the values match';
  } elseif ($string_one == "") {
    echo '$string_one is empty';
} else {
  echo 'the values DO NOT match';
}

I have also tried concatenating the string with a new line and I still have the same problem.

if ($string_one == 'I am Learning to display "Hello Alena!" to the screen.' . '\n') {
  echo 'the values match';
  } elseif ($string_one == "") {
    echo '$string_one is empty';
} else {
  echo 'the values DO NOT match';
}

What am I missing?

Thanks,

Shoaib Kamal Khan

Steven Parker
Steven Parker
229,608 Points

When using single quotes, the escape sequence is not recognized. So '\n' actually contains two characters, a backslash and a normal letter "n".

If you use double quotes ("\n") as you did on line 8, you'll get a single newline character and a match.

Shoaib Khan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Shoaib Khan
Front End Web Development Techdegree Graduate 21,177 Points

Thanks a lot Steven!!! That worked perfectly!!! Here is the updated code:

<?php
$name = 'Alena';
$string_one = "Learning to display \"Hello $name!\" to the screen.\n";
$string_one = 'Learning to display ';
$string_one .= '"Hello ';
$string_one .= $name;
$string_one .= '!" to the screen.';
$string_one = $string_one . "\n";
//prepend to a string
$string_one = 'I am ' . $string_one;
echo $string_one;

$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($string_one);

//($string_one == 'Learning to display "Hello Alena!" to the screen.');
if ($string_one == 'I am Learning to display "Hello Alena!" to the screen.' . "\n") {
  echo 'the values match';
  } elseif ($string_one == "") {
    echo '$string_one is empty';
} else {
  echo 'the values DO NOT match';
}

?>

Thanks,

Shoaib Kamal Khan