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 Browser Persistent Data with PHP Data Persistence on the Web Checking for Sessions

I do not know what is wrong with code? Does anybody see the issue?

for($i=0;$i<5;$i++){ if(empty($_SESSION['word'][$i])) { header('location:play.php?p=2'); } }

story.php
<?php
session_start();
for($i=0;$i<5;$i++){
  if(empty($_SESSION['word'][$i])) {
    header('location:play.php?p=2');
    }
}
$word1 = htmlspecialchars($_SESSION['word'][1]);
$word2 = htmlspecialchars($_SESSION['word'][2]);
$word3 = htmlspecialchars($_SESSION['word'][3]);
$word4 = htmlspecialchars($_SESSION['word'][4]);
$word5 = htmlspecialchars($_SESSION['word'][5]);

include 'inc/header.php';

echo '<h1>My Treehouse Story</h1>';

echo '<p>There once was a(n) ' . $word1;
echo ' programmer named ' . $word2; 
echo '. </p>';
echo '<p>This ' .  $word3; 
echo ' programmer used Treehouse to learn to ' . $word4;
echo ' the ' . $word5 . '.</p>';

echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>';
echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>';
echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';


include 'inc/footer.php';

4 Answers

Tianni Myers
Tianni Myers
10,453 Points

In the for loop you only declare the variable once.

for($i=0;  i<5; i++){
  if(empty($_SESSION['word'][i])) {
    header('location:play.php?p=2');
    }
}

Thanks Tanni that is something i was overlooking in the attempt to get the challenge done.

Dear Tanni Myers,

After trying your improved code i got other errors the variable $i must be used as $i otherwise the webserver will not know the variable.

Tianni Myers
Tianni Myers
10,453 Points

Oh okay it’s different for Js my bad.

Nothing wrong with your javascript skills just that this matter is php. Suddle little syntax difference.

Sorry Tanni,

But this right here is php the syntax is a little different from JS. That is what you are coming from. And when i get to the solution remember me post so everyone benefits.