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 Arrays and Control Structures PHP Loops Ping Pong

Anastasia Khmelevskaya
Anastasia Khmelevskaya
1,775 Points

Syntax error although I wrote the code exactly as in the video

Hi, when I try to run the code, I get the error: PHP Parse error: syntax error, unexpected end of file in /home/treehouse/workspace/pingpong.php on line 34

On line 34 I only have the closing php tag. Had the same error in the previous exercise as well (loops.php), so maybe it's some kind of workspace bug? Here's the code I wrote, please let me know if there's really a mistake there:

<?php

$player1 = 0; $player2 = 5; $round = 0;

//var_dump(abs($player1 - player2)); //var_dump($player2 - player1);

//player must reach a score of 11 //player must be a minimum of 2 points higher than opponent //WHILE those conditions are NOT met, a single player will receive 1 point at the end of each round.

while (abs($player1 - player2) <2 || ($player1 <11 && $player2 <11)) { $round++; echo "<h2> Round $round </h2>\n"; if (rand(0,1)) { $player1++; } else { $player2++;

echo "Player1 = $player1<br/>\n"; echo "Player2 = $player2<br/>\n"; } echo "<h1>"; if ($player1 > $player2) { echo "Player1"; } else { echo "Player2"; } echo "is the winner after $round rounds! </h>\n";

?>

2 Answers

Jeremy Caudle
Jeremy Caudle
23,719 Points

Hi Anastasia, it looks like you are missing a few characters (a *$** before the word player2 and a } at the end)* on this line in your PHP.

while (abs($player1 - player2) <2 || ($player1 <11 && $player2 <11)) { $round++; echo "<h2> Round $round </h2>\n"; if (rand(0,1)) { $player1++; } else { $player2++;

The code runs properly after adding those characters back in.

while (abs($player1 - $player2) <2 || ($player1 <11 && $player2 <11)) { $round++; echo "<h2> Round $round </h2>\n"; if (rand(0,1)) { $player1++; } else { $player2++; }

I hope this helps.

Jeremy Caudle
Jeremy Caudle
23,719 Points

It looks like I made a mistake with my Markdown, sorry about that. The character missing before the word player2 is only $, not the additional asterisks.

Anastasia Khmelevskaya
Anastasia Khmelevskaya
1,775 Points

Hi Jeremy, thank you so much, but after adding the missing $ in the code I'm still getting the same error: PHP Parse error: syntax error, unexpected end of file in /home/treehouse/workspace/pingpong.php on line 34

And on that line 34 I only have the closing tag: ?>

Jeremy Caudle
Jeremy Caudle
23,719 Points

Did you also add the right curly bracket } after $player2++; at the end of the line? I believe the missing curly bracket is causing the β€œunexpected end of file” error because the else statement has not been closed.