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

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,705 Points

PHP: Ping Pong Simulation Game

Just thought I'd share this version of the Ping Pong Game. Code is Alena's with 1 or 2 little tweaks of my own. What do you think? :)


<!doctype html>
    <title>PHP: Ping Pong Simulation Game!</title>

    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">


    /* Styles */
        * {
            margin: 0;
            padding: 0;


        body {
            background: pink;
            font-family: "Roboto Slab";


        header {

            width: 100%;
            height: 150px;
            background: blue;
            color: white;


        .pp_head {
            text-align: center;


        .content {
            line-height: 1.5;
            position: relative;
            width: 400px;
            margin: 20px auto;
            background: yellow;
            /*padding: 15px;*/
            box-sizing: content-box;


        div.rallies:nth-child(2n) {
            background: lightgreen;
            color: orangered;


        .rallies {
            padding-left: 20px;

        footer {
            text-align: center;
            margin-top: 20px;


        .logo {
            position: relative; 
            top: 20px;
            margin: 0 auto;
            display: block;
            width: 100px;
            height: 100px;
            border-radius: 100%;


        h2.winner {
            margin-top: 10px;
            padding: 10px;
            text-align: center;
            font-size: 17pt;
            text-transform: uppercase;
            color: red;

        p span {
            font-weight: bold;



        //initialise and declare variables for player and round scores.
        $player1 = 0;
        $player2 = 0;
        $round = 0;

        <img class="logo" src = "logo.png" />

    <div class="pp_head">
        <h1>PHP Ping Pong Simulation Game</h1>

        <p> Let's play!</p>

    <div class = "content">

        <div class="round">

                //player must reach a score of 11
                //player must be a minimum of 2 point higher than opponent

                //continue the game while the absolute value is less than ;
                while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11) {
                   echo "<div class =\"rallies\">";
                   echo "<h2>Round $round</h2>\n";

                   //random scores between 1 (max) and 0 (min)
                   if(rand(0,1)) {

                   } else {
                   echo "Player1 = $player1<br />\n";
                   echo "Player2 = $player2<br />\n";
                   echo "</div>";

        <div class = "winner">
                if($player1 > $player2) {
                   echo "<h2 class=\"winner\">Player 1";
                } else {
                   echo "<h2 class=\"winner\">Player 2";

                echo " is the winner after $round rounds</h2>"; 

        <p>PHP Ping Pong Simulation Game. From Teehouse by Jonnie Grieve Digital Media</a></p> 


Wow you took the game from a 1 to a 9... I love it.. You did a great job

Good! :smile:

Thanks for this.

//continue the game while the absolute value is less than ; while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11) Why do you use the logical operators where you do. In Alena's game, the score starts out with an absolute value of 5. So wouldn't the game be over if you use the || or logical operator where you do? If the absolute value needs to be greater than 2 for the loop to stop or the players must have a score of greater than 11 to win then I think the game should have ended before it began. Shouldn't the first logical operator be &&? And why is the second logical operator && shouldn't it be || since we only want one of the players to have a score greater than 11 to win? I am so confused by this lesson.

1 Answer

I think I know the answer to my question of why the certain logical operators where used.

while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11)

While either of the conditions are true then the loop will continue to run. Even thought the first condition is false (bc the abs starting value is more than 2) the second condition is true so the while loop will continue to run until both conditions are false.