1 00:00:00,580 --> 00:00:03,230 I'm going to show you some other loops that I prefer 2 00:00:03,230 --> 00:00:05,300 when it comes to looping through arrays. 3 00:00:05,300 --> 00:00:08,670 But I don't want you to think that while loops are unimportant. 4 00:00:08,670 --> 00:00:14,084 Let's create a simple ping pong game to demonstrate another use case for 5 00:00:14,084 --> 00:00:15,155 while loops. 6 00:00:15,155 --> 00:00:24,903 Let's create a new file named pingpong.php We add our PHP tags. 7 00:00:27,725 --> 00:00:30,788 And then we're ready to start programming the game. 8 00:00:30,788 --> 00:00:34,875 We'll start with two players, each with an initial score of 0. 9 00:00:36,150 --> 00:00:38,439 $player1 = 0. 10 00:00:38,439 --> 00:00:40,130 And $player2 = 0. 11 00:00:42,530 --> 00:00:45,950 I also want to track how many rounds each game takes. 12 00:00:45,950 --> 00:00:47,388 So I'm going to add another variable. 13 00:00:47,388 --> 00:00:52,470 $round = 0. 14 00:00:52,470 --> 00:00:53,910 Now let's add some comments. 15 00:00:55,320 --> 00:00:58,652 To win, a player must reach the score of 11. 16 00:00:58,652 --> 00:01:02,910 While also being a minimum of 2 points higher than their opponent. 17 00:01:02,910 --> 00:01:05,970 So while those conditions are not met. 18 00:01:05,970 --> 00:01:10,732 I'm going to randomly increment one of the player's scores each round. 19 00:01:10,732 --> 00:01:13,823 To check for the difference between the player scores, 20 00:01:13,823 --> 00:01:16,068 I can subtract one score from the other. 21 00:01:16,068 --> 00:01:18,750 This will give me a number that can be positive or 22 00:01:18,750 --> 00:01:21,380 negative depending on which score is higher. 23 00:01:22,750 --> 00:01:26,360 We can then find the absolute value of that number. 24 00:01:26,360 --> 00:01:28,020 Let me demonstrate. 25 00:01:28,020 --> 00:01:32,315 Let's change $player2 = 5. 26 00:01:32,315 --> 00:01:33,416 Then we'll do a var_dump. 27 00:01:37,988 --> 00:01:41,495 $player1- $player2. 28 00:01:41,495 --> 00:01:48,050 Then we'll also var dump $player2- $player1. 29 00:01:51,165 --> 00:01:52,406 Let's run the script. 30 00:01:55,734 --> 00:02:02,170 I see that the first var dump shows -5 and this the second var dump shows 5. 31 00:02:02,170 --> 00:02:06,250 The difference between these numbers is 5. 32 00:02:06,250 --> 00:02:10,820 All I care about is that the difference between these numbers is greater than 2. 33 00:02:10,820 --> 00:02:13,050 I don't care which is the higher number. 34 00:02:13,050 --> 00:02:18,384 So, I can use a function to determine the absolute value, abs. 35 00:02:21,790 --> 00:02:25,596 I'll add this to the first var_dump. 36 00:02:25,596 --> 00:02:26,900 Now, when I run my script. 37 00:02:28,810 --> 00:02:33,200 You can see that the absolute value gives me the number itself, 38 00:02:33,200 --> 00:02:34,707 without the negation before it. 39 00:02:36,040 --> 00:02:38,500 I can use this absolute value in my while loop. 40 00:02:39,720 --> 00:02:40,912 Let’s comment out these var dumps. 41 00:02:48,475 --> 00:02:56,255 So I want to continue to loop while the absolute value is less than 2. 42 00:02:58,950 --> 00:03:04,340 I also want to continue to loop while both scores are less than 11. 43 00:03:04,340 --> 00:03:06,468 So I'm going to add a second expression. 44 00:03:06,468 --> 00:03:11,772 I’ll combine these with the double pipe 45 00:03:11,772 --> 00:03:17,701 operator because I want either or to be true. 46 00:03:17,701 --> 00:03:22,149 $player1 is less than 11 and 47 00:03:22,149 --> 00:03:26,440 $player2 is less than 11. 48 00:03:26,440 --> 00:03:29,760 I need to put parenthesis around these two expressions so 49 00:03:29,760 --> 00:03:32,100 that they evaluate as a single conditional. 50 00:03:34,295 --> 00:03:37,905 $player1 must be less than 11 and 51 00:03:37,905 --> 00:03:42,190 $player2 must be less than 11 to continue my loop. 52 00:03:43,400 --> 00:03:48,240 So if the difference between the players is less than 2, we'll continue to loop. 53 00:03:48,240 --> 00:03:55,080 Or if $player1 and $player2 are both less than 11 we will continue to loop. 54 00:03:56,730 --> 00:04:00,720 The first thing I want to do is to start incrementing the round. 55 00:04:00,720 --> 00:04:02,320 This would be round 1. 56 00:04:02,320 --> 00:04:04,025 So adding 1 to 0 = 1. 57 00:04:07,522 --> 00:04:11,350 Let's also let the user know what's going on with each loop. 58 00:04:11,350 --> 00:04:13,070 So let's start with the heading for the round. 59 00:04:14,180 --> 00:04:17,196 I want to view this in the browser, so let's add the HTML tags. 60 00:04:20,683 --> 00:04:26,521 Round $round, And close my tags. 61 00:04:29,404 --> 00:04:33,330 Notice that I used the double quote, so that my variable is expanded. 62 00:04:34,700 --> 00:04:40,926 Now we want to randomly add a point to either $player1 or $player2. 63 00:04:40,926 --> 00:04:46,730 PHP has a function named RAND that will generate a random integer. 64 00:04:46,730 --> 00:04:51,190 We can specify a minimum and a maximum for this random integer. 65 00:04:51,190 --> 00:04:56,391 So we can say if {rand(0,1) 66 00:04:56,391 --> 00:05:01,460 then we're going to do something. 67 00:05:02,570 --> 00:05:08,140 The min and max are inclusive, meaning it will include the minimum and 68 00:05:08,140 --> 00:05:11,970 the maximum in its random number generation. 69 00:05:11,970 --> 00:05:14,820 With a minimum of 0 and a maximum of 1, 70 00:05:14,820 --> 00:05:19,810 we will randomly receive either 0 or 1. 71 00:05:19,810 --> 00:05:24,290 Since 0 evaluates to false and 1 evaluates to true, 72 00:05:24,290 --> 00:05:27,280 we can use this in our conditional. 73 00:05:27,280 --> 00:05:31,836 If 1 is returned and our expression is true, we can add 1 to $player1. 74 00:05:35,374 --> 00:05:38,845 Else we can add 1 to $player2. 75 00:05:42,193 --> 00:05:44,481 Now, let's display the current score for this round. 76 00:05:47,916 --> 00:05:53,669 Player1 = $player1 score and a line break 77 00:05:58,257 --> 00:06:05,058 Player2 = $player2 score and a line break. 78 00:06:05,058 --> 00:06:07,870 And that's all we need for a while loop. 79 00:06:07,870 --> 00:06:09,290 So let's preview this script in a browser. 80 00:06:12,053 --> 00:06:15,990 We can see each round with a point being awarded to a random player. 81 00:06:17,420 --> 00:06:25,031 And the final score ending with Player1 = 2 and Player2 having 11. 82 00:06:25,031 --> 00:06:27,460 This is okay, but let's add a final summary. 83 00:06:29,540 --> 00:06:34,840 After our while loop, we should add our comment back up in the while loop. 84 00:06:40,043 --> 00:06:48,120 And after our comment we can add echo header 1. 85 00:06:48,120 --> 00:06:56,236 If $player1 > $player2, 86 00:06:56,236 --> 00:07:02,488 we'll echo Player1. 87 00:07:02,488 --> 00:07:07,660 Else we'll echo Player2. 88 00:07:10,520 --> 00:07:14,208 And finally we'll echo, 89 00:07:14,208 --> 00:07:19,410 is the winner after $round rounds. 90 00:07:20,850 --> 00:07:21,480 And close our header. 91 00:07:24,270 --> 00:07:26,640 Let's preview our script again to see the final result. 92 00:07:29,460 --> 00:07:31,780 Player 2 is the winner after 9 rounds.