در حال بارگذاری ...

Need Lists of Data0:00

Alright, so check this out. At this point you should be pretty comfortable with creating variables for primitive strings and numbers, right? name equals John Doe, age equals 25. But now, what about when the variable should contain, so to speak, a collection of things, like a list of your favorite books? How exactly would we do that? I'll show you how. Alright, so let's say you want a section of your website for recommended books. So maybe you'd have an unordered list, and just for fun I've chosen three different books.

Okay so now our job is to make this dynamic. Remember in real life things like this will often be stored within a database. Here's John's recommendations, or here's Sarah's recommendations. All of those are stored in a database. So yeah, how would we do this? Well, once again, we could open up php directly within our body tag. And yeah, what would be a good variable name for our list of books? Well I said books, right? So why don't we call it books. But yeah, at this point we've learned that we can do things like this, but I instead

Introducing Arrays2:01

So why don't we call it books. But yeah, at this point we've learned that we can do things like this, but I instead wanted to represent many book names. So what do we reach for in that case? And the answer is an array. So think of an array as the programming equivalent of a folder. It's a way to take a group of things or a list of things and put them together in a folder. And then if you want to move all of those things from one place to another place, you just pick up the folder and you carry it, right?

And then if you want to move all of those things from one place to another place, you just pick up the folder and you carry it, right? The same basic principle is true for arrays. I create an array using this notation. Open bracket, closed bracket. Now I can store each of these titles as strings. There's one. Next the Langoliers. And then finally Hail Mary. All right, cool.

Looping with foreach2:49

And then finally Hail Mary. All right, cool. So now I have a single variable that represents a list of recommended books. The next step is to loop over that list and for each item display a list item that includes the name of the book. So here's how we do that in php. Let's do this. Open up php and I will say foreach books as. Now when I'm looping over this array, how do I want to represent or refer to each item within that array?

Now when I'm looping over this array, how do I want to represent or refer to each item within that array? Well, how about name or book? Anything you want. Singular is fine. And then just like with conditionals in the last episode, we open up {}. Okay. And I'll close php at the bottom. So now the way this works is for however many items we have in this array, I will trigger all of the logic that is contained between these {}.

So now the way this works is for however many items we have in this array, I will trigger all of the logic that is contained between these braces. So if you want to pause and think to yourself, well, how many times will I... Let's do this. echo a list item that says hello. Okay. So if I were to run this in the browser, pause for a second and think, how many hellos will I see? Okay. Well, as you can imagine, we will see three hellos.

Echoing Array Items4:18

And now let's replace the text here with the name of the book. So I'll show you a couple of ways to do this. We could once again use concatenation. So in this case, I'm saying, okay, I want to render a list item and then the name of the book and then a closing list item. And that should in fact do the trick. Pretty cool. But of course, as we learned an episode or two ago, we could also just inline all of this. I could say echo, open a list item, then do the name of the book, and then close it all.

this. I could say echo, open a list item, then do the name of the book, and then close it all in one go. Come back, refresh, and very good. But now a quick little note. I don't know if we covered this, but imagine for whatever reason, you need to add that little trademark symbol at the end of the book name, something like that. But notice how my editor immediately starts complaining at me. And that's because now it's trying to find a variable called bookTM, and of course that doesn't exist.

And that's because now it's trying to find a variable called bookTM, and of course that doesn't exist. So yeah, of course the issue is I want that TM to occur immediately after the name of the book. So I could add a space here, but in this situation, I don't want that. I don't want a space between it. So what do we do in situations like this? Well, of course you could reach for concatenation, or you'll often see this in the wild. You can wrap the name of the variable within {}. And that sort of silos the variable.

Alternative foreach Syntax5:55

Okay, so for little examples like this, what we've come up with is perfectly fine. But yeah, as you can imagine, in some situations, your list item will contain its own set of HTML. Maybe it houses a div with h1 and h2 tags and anchor tags and things like that. And I think what you'll find is when you start trying to render all of that as part of an echo string, as you can imagine here, very quickly it starts to get super messy. So for this reason, there is a shorthand for loops, and I'll show you how it looks. Let's start from scratch. Open up php, and yeah, foreach $books as $book, but this time instead of opening up a curly brace, we're going to use an alternative syntax.

Open up php, and yeah, for each books as book, but this time instead of opening up a curly brace, we're going to use an alternative syntax. Sort of a shorthand that you will mostly see when working with views or HTML, as you see here. And that shorthand or alternative syntax is a colon. Okay, so let's close php so that I can return to HTML. Hello there. And then we need some way to delineate where the end of the foreach should occur. So at the very bottom, I will open up php again and write endforeach. All right, so if we switch back, we should once again get three hello theres, and we

So at the very bottom, I will open up php again and write endif for each. All right, so if we switch back, we should once again get three hello theres, and we do. Okay, so I think you'll find whenever you need to build up complex HTML fragments, this alternative syntax for foreach is 100% the way to go. And yeah, it's not limited to foreach. You can also use it for conditionals. It's the exact same syntax. Okay, so now let's just update this by opening php, echoing out the book name. And if I switch back and refresh, we're good to go here.

Okay, so now let's just update this by opening php, echoing out the book name. And if I switch back and refresh, we're good to go here. But also don't forget, you learned about short echo tags, right? So you could instead just say, = book, and then close it out. And that would work as well. And once again, this is the approach that I would usually take. So give it a refresh, and we still get the same thing. All right, so you're making pretty good progress now. You've now learned that a variable can house, so to speak, a collection of things, and that's specifically what an array is for.

You've now learned that a variable can house, so to speak, a collection of things, and that's specifically what an array is for. It's for containing a collection of things. Maybe that collection could be names, a collection of names. It could be a collection of numbers. It could be a collection or list, if you prefer, a list of usernames, a list of book names, a list of popular tweets, anything like that could go in a primitive array. And then you use the foreach syntax to loop over each item within that array and proceed however you need to.

ArraysForeachAlternative Syntax

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟