Now let's learn the first loop, which is the while loop.
So the loops are different from the if statement in the way that with the if statement.
If the expression in the condition is true, the statements in the body are only run once.
Now, with loops like the while loop, the statements will be run.
As long as this condition is true, this might be infinite number of times.
So that's also something you need to watch out for that you might just create an infinite loop.
Okay, so let's see a practical example of a while loop.
So back in the code editor let's create an example with the while loop.
Now I would like this to be something interesting, as I don't like to learn with boring examples myself.
So let's create a simple guessing game That would run in the command line, and the job of the user
would be to guess our password.
So we need a variable where we're going to keep this password.
Let it be magic.
You can come up with something else.
Then let's add a variable to count the attempts that someone has made.
Start with zero.
Then let's decide on the maximum attempts before his luck is running out.
Let it be five.
And now the game.
So the while loop.
That was pretty simple.
We've got the while keyword parentheses that's mandatory to add them, and the expression that needs
to go inside the condition.
So this would be rather simple.
If the attempts is less than maximum attempts, we run the code that is inside this loop body.
So what could this be?
Well we should first start with a question.
So let's give them a chance to guess the password.
So we echo that on the screen.
So next we need to take the input from the user.
Let's store it in a variable.
And we also need to forget for a second that we haven't learned about the functions yet.
And the only way to accept the input would be to call this specific functions.
So let me just type that and we're going to talk about this later on.
So those two functions basically they take the input from the command line and trim the white spaces
okay.
So at this point we are asking for the password for the user to guess the password.
We take his answer.
And since we use this attempts to figure out if we should still let him try again, we need to increase
the attempts.
So in PHP you have this plus plus operator that basically is increasing an integer by one.
So we need to do that.
Remember about infinite loops.
If we would forget to increase this attempts variable value, we might end up with a loop that just
runs forever.
Okay, so now we need to take some decisions.
Fortunately we know the if statement so we can see if maybe the guess is identical to our secret.
So if that's the case we're just gonna echo.
Correct.
You've unlocked the treasure.
Let's add a new line character, and maybe I'm gonna put some special symbol so it looks nice.
Let me search for a diamond.
You can copy those characters Actors from the code resources that are always on GitHub.
Now we need also an alternative.
And if the attempts are equal, the max attempts.
Well, we need to tell to the user that that's the end of the game.
So let's do echo out of attempts.
The treasure.
Remains locked.
Newline character.
And let's also add a symbol.
Um, maybe just a lock.
I think this would be fine.
Okay.
Semicolon.
Next up, an alternative.
So what can be an alternative?
Either someone has guessed if the guess is different than the secret, then we just check if that was
the last attempt.
But the alternative to this is that wasn't the last attempt and the guess was incorrect.
Which leaves us with saying that someone was wrong.
But he can try again.
And we also should say that there are some attempts left, so attempts left equal.
Now we use the concatenation operator that we already know, and let's close that in parentheses and
subtract the attempts from max attempts.
So we are doing just simple math here.
And let's make sure we add a new line character.
And this is actually the whole game.
One more thing.
So if someone would guess the secret then we actually need to stop this loop, because the way it works
is this loop will run the code that it is inside the body for maximum of five times.
We've got five attempts.
But if we would like to stop this loop earlier because something has happened, like in this case someone
has guessed the password, so there is no point in asking about the password.
Let's say two remaining times.
We can use the break statement.
It will just exit this loop.
Okay, so it's time to try this out.
I think everything should be fine here.
So the password we have it here.
Let me type something that's wrong.
We've got.
Okay.
So we are out of attempts and we see that everything worked fine here.
So let me try again.
But this time I'm gonna type the proper password.
Magic.
We've got the correct success message.
And as you see, the program stopped.
Which also means that this break statement has successfully, well, basically broken us out of the
loop.