PHP Basics

Loops

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Loops" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates the while, do while, for, and foreach loops. Like other languages, curly braces contain the body of the loop. PHP has an alternative syntax to add a colon at the beginning of the loop and an identifier like endforeach to terminate the loop.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Loops" Lesson

[00:00:00]
>> Maximiliano Firtman: Now, if you have an array, how to go through the, how to loop through the array, we have while, as you are used to, a while has need a Boolean condition in parentheses. Okay, so coming from some languages where it's not mandatory, here it's mandatory. You just have do while.

[00:00:17]
Okay, that executes. Always first and then evaluate the condition to see if it's do another loop or not. But for this case, I think if you wanna render all the countries, it's verge have for, right? We do have the normal for the, let's say the C based for the int i equals 0 or bar i Javascript.

[00:00:41]
Well, now we don't define a type, or bar, or let, we just say bar $i= 0, i less than count of countries.
>> Maximiliano Firtman: Do I have i++? Yes, we do have plus plus, but it said, hey, you shouldn't be using here. Why? And this is something, do I have plus equals one?

[00:01:11]
>> Student: You need the dollars in front of the other is.
>> Maximiliano Firtman: I need, yeah, you're right.
>> Student: Okay.
>> Maximiliano Firtman: You're right. Thank you for that. That's why I mentioned C or JavaScript. My brain, it says, you wanna write JavaScript. So then it jumps to the JavaScript part. Thank you.

[00:01:29]
So you can use a plus plus or plus plus Y, as I mentioned. The order, in this case, it won't make a difference. But in other cases, when you have a property with a getter, and the ordering with first it tries to set plus one and then it gets, or it gets and, I don't wanna get so deep there because it's not really important here.

[00:01:50]
But we do have a foreach. But the foreach is different to other languages. I mean, not so different. The nature is the same. But you first, this is a partly different. The first variable you define here is the collection, not the element that you will be using for the loop.

[00:02:10]
So, first, I use the collection. Then we are using the as keyword and we create one or two values. For example, in this case I can say country in singular. And for example I can do an echo of the country, okay, makes sense? So this is actually this will loop through all the countries.

[00:02:36]
So we say for each country as this is in JavaScript, this is if for off in JavaScript, right? Just to give you an alternative, in JavaScript, you say, for each country, of countries. You see in php, we reverse the order. We first add the collection, and then we use as and then the value.

[00:03:01]
If you have only one expression here, is actually the value. But for example, VS Code here, when I was typing for each was actually suggesting me this syntax. You can get also key. For example, in this case, the key will be the index or the I, if you wanna call the I.

[00:03:21]
So, if you want both, I can now look at this. I can put this in a string, and I can say, i : country. Does it make sense? So if I try to execute this, we will stop executing from the terminal in just a few minutes. So I can do php arrays, oops, arrays.php.

[00:03:52]
You actually need to add the .php when you are executing this. And I'm getting 0: Argentina, blah, blah, blah. You see I'm not getting the new line because if you want a new line in the output, you have to manually add the new line, \n. And finally, I have that output.

[00:04:18]
>> Maximiliano Firtman: Okay, make sense? Yep
>> Maximiliano Firtman: There is an alternative syntax, because, right now, we are just making an output. It's just a one PHP file that I'm pretty sure that you are really comfortable with this. But it was pretty common before of doing this, I close the tag here, and then I open the tag again here, php.

[00:04:50]
So then I don't need to do an echo. I do this.
>> Maximiliano Firtman: And, of course, then I open another tag here. That was pretty common before. I can also use a shortcut, but I don't wanna mess too much with shortcuts here. This and I see your faces. Why this was most common before?

[00:05:17]
Because we will write HTML and it was better to see the HTML. Here maybe we have a line with an article, and the article has an h1, and then this one goes inside. So it's better to write HTML outside of PHP and then say, well, in this place, how do I want PHP, okay?

[00:05:39]
Okay, makes sense? Well, this should work. Of course, I'll put in this in the console. It's not nice, okay, but it works. You can see I'm seeing different analyze with different articles, so it works. This is more for the browser, okay? But the problem with this, it works.

[00:05:57]
I mean, I was creating a lot of code like this. The problem is this, closing the code block. You, after a while, because here you can have another PHP that has another for, then you don't know after a while which language element you're closing.
>> Maximiliano Firtman: Okay, so you get lost quickly.

[00:06:25]
So that's why in modern PHP, so if you have experience with PHP in the past, this ain't new, you can remove the code block and add a colon. And then instead of closing the code block you use, endforeach;
>> Maximiliano Firtman: It's optional. It's an alternative syntax, okay? That is useful for when you are mixing output with PHP and it's not chat for each.

[00:07:04]
We have this for while, for if, for all the flow control structures.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now