Defining Variables0:30
2. Okay, so let's do our review. And first up, if I want to define a variable, I always begin with a $ dollar sign. So let's create a variable for a businessName. Done. Notice the name is a string, so we surround it in quotes, and then we conclude the statement with a semicolon. Let's make another variable for the cost of Lerikas, which is $15 a month. Now as you can imagine, sometimes we need to gather and group all of these attributes. So in those situations, we can switch over to an array.
Grouping Data in Arrays0:56
Now as you can imagine, sometimes we need to gather and group all of these attributes. So in those situations, we can switch over to an array. business equals an array. The name of the business is Lerikas, and the cost of the business, once again, is $15. Now I can access each of those values by doing this. business[the name of the key]. Next, let's move on to conditionals. Maybe if the monthlyCost is too high, we don't want to proceed. We could say, if businessCost is greater than, how about, $99, in that case, yeah, let's leave the store.
Conditionals and Loops1:29
We could say, if businessCost is greater than, how about, $99, in that case, yeah, let's leave the store. Echo, not interested. Easy. All right, let's do another one. What about the categories that are offered as part of the business? For Lerikas, that would be things like testing, PHP training, JavaScript training, et cetera. Next we want to loop over them. All right, that's easy too. Let's use foreach.
All right, that's easy too. Let's use foreach. foreach, business, categories, as, category, then once again, render it however you need to. In this case, we'll do an echo. echo the category to the page. But yeah, in real life, you might want to render it as part of a list item, or at the very least, add a break. Finally, we reviewed functions. Maybe we need to offer users a way to sign up for our site.
Creating and Using Functions2:22
Finally, we reviewed functions. Maybe we need to offer users a way to sign up for our site. Well, in those cases, very likely we'd have a function called register. Maybe that function accepts a User. And now, yeah, you can proceed however is appropriate. So I'll tell you, in real life, this will consist of things like create the User record in the database. Maybe sign them in. And that would involve something called sessions that we haven't yet reviewed. Maybe you want to send them a welcome email.
And that would involve something called sessions that we haven't yet reviewed. Maybe you want to send them a welcome email. Send a welcome email. Maybe you also need to redirect the user. So they just signed up. Let's send them to their dashboard. So redirect to their new dashboard. You get the idea. In fact, this is often the sorts of things that you will be doing in real life. OK, so that's functions.
Requiring Views and Includes3:09
In fact, this is often the sorts of things that you will be doing in real life. OK, so that's functions. Finally, we learned about requires and includes, and the importance of separating our logic from the template or the view. So in this case, I have a variable called business and a function called register. Let's now require our view, index.view.php. And that required file will have access to both of these. OK, so let's come back. And yeah, very quickly, let's echo the business's name. And then let's also include a list of categories that the business provides.
And yeah, very quickly, let's echo the business's name. And then let's also include a list of categories that the business provides. All right, easy enough. We could say for each business categories, and we'll call it category. And then I will use the short alternative syntax like so. And yeah, then within a list item, we can echo the category itself. OK, let's have a look in the browser. And yeah, sure enough, we have our business name as well as an unordered list of categories. And yeah, that'll do it for our rapid fire review of everything we covered in this chapter. So now your job before you move on to section two is to take the quiz and make sure that
