Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.