Placeholder Image

字幕列表 影片播放

  • So let's talk about one of the ways that you can loop in PHP called the Do while Loop.

  • So this is a little bit different than the other loops, and the main difference in a do while loop is that a boot?

  • It will run at least once.

  • So with the other loops they checked to see if the condition is true or false.

  • If a condition is false, then it simply gets skipped over.

  • It's kind of almost like an if statement.

  • If this is true, then do the loop.

  • If it's false, don't do anything.

  • What happens to the do while loop is you say, do you give the code to run?

  • And then at the end of it, you say, while the condition right so while the condition is true, continued, a loop will by putting the while at the end.

  • What that means is the code will run at least once, so if you want to make sure it will run at least once for some reason you would use a do while loop versus a while or a four loop.

  • So with that, let's go over to the computer so I can show you how this works.

  • So this is a simple script that I created called a duel while dot PHP to show you what a do while look, loop looks like.

  • And basically, if you look at a do while loop, it almost looks like a while loop in reverse, which essentially is what it is.

  • The important difference between the do while versus the other loops is that this will run at least once.

  • So again we start with PHP tag, and then we're going to create our variable.

  • So we have variable, we create called dollar Sign X.

  • We're going to set the value of variable to one and then close with a semi colon.

  • Then down here, what we're going to do is we're going to say do and then we're gonna go straight to the squiggly brackets against straight to the squiggly brackets.

  • So we're going to run this at least once and then for here.

  • What we're going to say is, we're gonna say print Hello, Time number X.

  • So basically saying how many times we've said hello, and then what we're going to do is we're gonna print the html break so this will break to another line when it runs, and then at the end again, we're going to do wth e increment ation up.

  • So we're going to say dollar sign, X plus plus So what this does is it takes the value of X, it adds one to the value of X, and then it sets the value of X toe.

  • What that is so one plus one equals two exes now equal to two plus 32 plus one equals three.

  • So now X equals three.

  • And so it keeps auto implementing up that way.

  • And then what we're going to do is we're then gonna close this squiggly bracket so everything between the squiggly brackets are going to be done.

  • And then we say, while parentheses dollar sign X is less than or equal to 10 close parentheses.

  • Semicolon.

  • And so this is what the do while loop looks like.

  • And again, relatively pretty simple.

  • Ah, Then at the end, we close the PHP tag and that's all there is to it.

  • If we g o over too, the browser do this, we go to do while and then we can see.

  • Hello.

  • Time number one.

  • Hello.

  • Time number two.

  • Hello.

  • time number 345 So on and so forth.

  • So again, if we go here and we change the value of X to, let's say seven, we have file save, we then upload and then we refresh.

  • Then we see it'll go.

  • Phil, start at seven.

  • Hello?

  • Time number 789 10.

  • It keeps going now.

  • The important thing to understand about the do wild loops, though, is what if we set this to 80?

  • So we set this really high.

  • So this is obviously above 10 so 10 is greater than ah, 80 is greater than 10.

  • So we're gonna do is we're gonna save, and then we're going to upload, And then we will do free, fresh and see.

  • Now it says, hello, Time number 80.

  • So it goes through that set of code once, but then when it hits the while, it fails out there, and so it won't continue.

  • So this right here this is the a big difference between the while.

  • The four and the do wild loop that do while will run the code at least once.

  • So that's all there is to do while loops again.

  • One of problems you can run into is people.

  • Forget the auto increment ation, and so you can have the loop that just keeps going and going and going and going to go in.

  • Or you can run into a problem where if you're not thinking about it and you use a do wild loop where you really should use a while or a four loop again, it will run at least once.

  • This could be a real problem with safe.

  • You're printing out reports if you're doing something.

  • This is where you know, sometimes you open up a report on a Web page, and then you kind of get like a random.

  • You get like a random record.

  • So you think you're supposed to get 20 records about something or 30 records about something, and instead you just get like one random record at the top.

  • The reason for that is her summaries, and the person used to do wild loop and so it will go through.

  • So even if the conditions that previous conditions aren't true or shouldn't output something for you, it will it will output that information just because it has to output something once and so enough things were true it's able to output basically this garbage to you once, and then you sit there scratching her head, thinking, Wait a minute.

  • I thought it was supposed to get 50 records here, and it said I'm getting a single random record.

  • What is that?

  • The reason is they used to do Wild Loop and it's giving you something because that's that.

  • That's what I do.

  • While Luke done says, give something, run the code and then, uh and then check to see on the condition.

  • And so that's one of the reasons why you can run into issues with do wild loops.

  • So that's what I do while Loop is, and that's why it matters.

So let's talk about one of the ways that you can loop in PHP called the Do while Loop.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

PHP - Do While Loops (PHP - Do While Loops)

  • 5 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字