PHP Front To Back
Lección 5 de 22 · PHP Front To Back [Part 5] – Loops · TI y software
PHP Front To Back [Part 5] – Loops
hey guys welcome to part 5 of PHP front to back in this video we're going to get into loop okay so loops are part of virtually
every programming language and what they do is they execute a block of code a set number of times okay based on some kind of condition so let's go ahead and in our PHP sandbox folder we'll create a
new file and let's call this loop dot PHP and throw in our PHP tags okay so this is going to be on root and like I said we'll say execute code set number of times okay and sometimes you know
what that number is sometimes you don't now there's four different types of loops so we have a for loop we have a while loop a do while loop and a for each loop so we're going to start off
with a for loop now for loop is usually used if you know the number of times it's going to be executed and it has a couple programs so let's say at Ram and it takes in an initializer and then the
second program is a condition which could be anything really and then increment okay so let's do four and so the initializer which we're just going to pretty much just create a variable
usually called I that's the calling practice annual step that's a zero initially and then each parameter is separated by a semicolon alright so next is the condition
so we'll say as long as I is less than 10 okay that's our condition and then the increment we're going to do I plus plus which is going to increment it by 1 okay so after every time it goes around
zero is going to increment by one up to ten and then it will stop because this will be true okay it'll-it'll process whatever we put in here anything at all until this is true
so what we'll do is just just echo out I so if we save that and we go to localhost / PHP sandbox / – oops PHP okay and it gives us zero through nine if we want to we can add a line
break here and put each number on its own line now notice that it only goes up to nine that's because once this is true it stops and we're saying if it's less than ten if it went ten if we went to
ten that's not going to work because this is less than ten if we wanted to know we could say less than or equal to ten and then if we reload it'll it'll include the ten okay and we can do
whatever we want here let's say we wanted to add the text number space and then we'll concatenate it like that and reload and we get number zero – number ten and we can also start this wherever
we want and say if we started at five then it's going to do five through ten okay so that's a basic for loop the next kind of loop we have is a while loop okay and then as far as params
parameters it just takes a condition that's it okay setting the variable and all that happens outside of the loop so let's do I equals zero notice that we're doing it
outside of the loop and then we're going to have wild and then that takes a condition so we'll say as long as I is less than 10 okay so same condition I'm actually going to comment this out up
here because I don't want that to run all right so while I is less than 10 let's go ahead and just echo out I and then echo a line break loose okay and then we
want to increment it while we're in here so I plus plus and then if we save that and reload you can see it does the same thing okay so a while loop is is usually used over a for loop if you don't know
the set number the set condition all right now there's also a do-while loop which isn't used as much I really never use do I loops but just to show you let's let's just say do-while and Czar's
quran the while in the do-while takes a position okay so what we'll do is we'll set we'll set the I variable and then we're going to say Q which doesn't take which
doesn't take parenthesis and here is what we want it to do now the main difference between this is that with a do-while loop it's always going to run at least once okay no matter what so
let's say X echo I and it's also actual a line break okay and then we're going to do our increment inside of the do so I plus plus and then down here we'll have the while with the condition
so we'll say while I is less than 10 okay so notice the wild doesn't have the curly braces all the action happens we then do okay we just put the condition and wild down here so let me comment
this stuff out and we'll go ahead and run this and again we get 0 39 okay so that's a do-while loop now the last type of loop we're going to look at is a for each and for each is different because
it's meant to work with arrays okay so let's create an array called people just like we did in the last video and we'll set it to array let's say Brad Jose and William so we want to be able to just
loop through this array and then access the values so for that we'll say for each actually put my little comments up here okay so for each for a raise okay so we want to say for
each and then we're going to pass in the name of the array which in this case is people and then we want to do as and then a variable that we want to use for each one so usually you would do
whatever the singular form of this okay so plural singular for each people each one is a person go down here let's just echo person and then we'll also ethical a library so let's try that and
you can see it just prints out each name you can do whatever you want with this variable through each iteration so that's a simple indexed array let's try it with a an associative array so I'm
going to copy that and then comment it out and then let's say we have a name as the key and then an email addresses the value so we'll do that and then Jose and William
okay so now we have an associative array now what we want to do is say for each people as person and then we want to put this equal sign greater than so this is basically going to be the key and then
this will be the value so we'll say email and you can use whatever variables you want but this is the format when you're looping through an associative array so let's say we want to echo out
person and then we'll just concatenate and put a colon in there and then concatenate on the email save that reload and we get the name which is the key and then the email which is the
value all right so that's going to do it for loops in the next video we're going to take a look at conditionals and look at some of the operators in PHP
In this video we will look at PHP loops. There are 4 types we will look at…
For Loop
While Loop
Do..While Loop
Foreach Loop
CODE: Code For This Video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox3.zip
COURSES: Premium Eduonix Courses
http://www.traversymedia.com/eduonix-courses
SUPPORT THIS CHANNEL WITH A CUP OF COFFEE PER MONTH:
https://www.patreon.com/traversymedia
ONE TIME DONATIONS:
https://www.paypal.me/traversymedia