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

Syntax error??

$i = 0;
foreach(htmlspecialchars($_SESSION['word']))
{
  $i++;
  if(empty $_SESSION['word']['$i'])
  {
    header("Location: play.php?p=".$i);
  }
}

How could there be a syntax error in the above code? the syntax error would persist at the for last closing braces.. hmm :/

2 Answers

few things i noticed.

a. in php foreach need a as value. b. if isset && !empty for the $ _session

$i = 0; 
foreach(htmlspecialchars($_SESSION['word']) as $value) {

 $i++;
 if(isset($_SESSION['word']['$i']) && !empty($_SESSION['word']['$i'])) {
    header("Location: play.php?p=".$i); 
    }
  }

hope this helps

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

A couple things

1) "empty" needs parenthesis

if(empty($_SESSION['word']['$i']))

2) htmlspecialchars should be used on the individual items, not the $_SESSION['word'] array.