Building the Task Array 0:00Okay, welcome back. Now, in general, the homework for each video is meant to be done on your own, and we won't always answer it. But in this lesson, let's work the homework into the discussion. So, I asked you to create an associative array and assign it to a task variable. And this should consist of the title, the due date, the assigned to value, and then finally a completed, so kind of like a true or false value. And we haven't talked about that yet. All right, let's do that now. What is the title of the task? How about finish homework? When is it due? Now, when you get a little more advanced, you'll learn about timestamps, or date times, and different ways to represent a date to a very accurate degree. But for now, we can just use the string today. That's okay. Okay, next, assigned to? Maybe your task application allows you to assign a task to yourself or anyone on your team. We're just going to say me in this case, Jeffrey. Finally, completed? Introducing Booleans 0:45That's okay. Okay, next, assigned to? Maybe your task application allows you to assign a task to yourself or anyone on your team. We're just going to say me in this case, Jeffrey. Finally, completed? Is the task finished or not? Okay, well, I told you you could do yes or no. But in programming, that's not useful. We're using the string no or the string yes. There's no value to that. It's just a string. So, what we want here is what we call a Boolean, kind of a scary term that's very, very simple. A Boolean is simply this, true or false. And this is kind of the bedrock of programming. So, when you are writing scripts, frequently, you'll need a trigger code based upon some kind of condition. If the value is this, then I want to proceed in this way. Or if it's that, then we want to proceed in another way. So, using this example, maybe you want to say, if the task is completed, so if the value is true or truthy is how we might say it,then we want to proceed in another way. So, using this example, maybe you want to say, if the task is completed, so if the value is true or truthy is how we might say it, well, maybe we want to add a green little checkmark in our HTML. Or if it's not completed, we don't want to display that green checkmark. We want to do something else. So, that's us using a condition in combination with a Boolean value. But now, why again is true or false better than yes or no? Because think about it, you could always just say, if this completed key is equal to yes, and in fact, you'll learn we use two equals there, but don't worry, we'll get to that when we talk about conditionals. Anyways, yes, you could do this. So, if completed equals yes, then display the checkmark. And if it is equal to no, then don't. But yeah, generally, this is just a bad practice. We are trying to break this down to a single value. Is it true or is it false?then display the checkmark. And if it is equal to no, then don't. But yeah, generally, this is just a bad practice. We are trying to break this down to a single value. Is it true or is it false? So, we have things in the system, in the language specifically for this, so we should use it. So, we will set completed to false. Now, another reason why we do this is so that we can say, once again, don't worry about the syntax, I'll teach you all of it. But you could say, if completed, assuming that was a variable. So, if this is true, then proceed. Or you could say, if it is not completed, then proceed in another way. But if we did completed yes, well, this string, that equates to a truthy value. And even if it's set to no, that's still a string that is populated with characters, so that would be called a truthy value as well. So, once again, are you seeing it? It just gets kind of blurry. We want to use Booleans for this, so we will. Okay, so why Looping to Display Data 3:16populated with characters, so that would be called a truthy value as well. So, once again, are you seeing it? It just gets kind of blurry. We want to use Booleans for this, so we will. Okay, so why don't we display this information on the screen. Okay, so if we switch to our view, I've cleared it out again, and we'll say, task, maybe we have one task per day. Task for the day. Okay, so once again, we're filtering through an array, so an unordered list would be good. So, how do we want to represent this? Once again, we want the key. So, we could say, within a strong tag, echo out the key. But remember, how do we get access to the key? Well, we have to loop through the array. Remember, for each item within this array as key value. Okay, let's wrap this. For each item within the task array as the heading, and maybe the value, then create a list item where we echo out the heading, and then I will close this out. Now, generally, just like with HTML, you want to followthe task array as the heading, and maybe the value, then create a list item where we echo out the heading, and then I will close this out. Now, generally, just like with HTML, you want to follow good indentation habits with your PHP. So, this is sort of like a block, so I would indent the list item here. Okay, so if we go to Chrome, there we go. Here's the heading, and then we will say a colon, and then I will echo out the value. All right, refresh, and there we go. You have your first task, and you can even style this with CSS to make it look like a little notecard. This looks good. But now, one little thing. Title, due date, assigned to, and completed, they're lowercase. It might be nice if they were formatted a bit better. Maybe the first letter is capital. Okay, well, yes, you could come back here and update this. But often, once again, this data is coming from an outside source. Maybe it's coming from something called an API. You'll learn about that Formatting Keys with ucwords 5:03well, yes, you could come back here and update this. But often, once again, this data is coming from an outside source. Maybe it's coming from something called an API. You'll learn about that later in your career. Maybe it's coming from a database where you can't change it. So, this is where you have a number of little helper functions in PHP. For example, I could wrap our heading in a function called UCWords. Now, this is shorthand for uppercase words. So, if we come back and refresh, notice that now the first letter is capital. Let me show you another example. If I say, UCWords, hello, my name is Jeff, it's going to apply to each word we have. So, you'll see that every word starts with a capital letter. Now, one thing that can get a little overwhelming is, well, how do we know that this function exists? And the answer is, lots of reading, and then lots of Googling. So, for example, if you didn't know that functionget a little overwhelming is, well, how do we know that this function exists? And the answer is, lots of reading, and then lots of Googling. So, for example, if you didn't know that function exists, then you will Google PHP capitalized first letter. And what you're going to find is just about every question you could have has already been asked. So, in this case, PHP capitalized first letter of each word. That's what we want. And here we go. So, you can either look for something called stack overflow. You'll have tons of answers there. Or, of course, for simple functions, you might go directly to the php.net website. So, let's take a look at that. Aha, UCWords is what we want. It shows us how to call it. So, you're going to give it a string as well as any delimiters you might want, but they're optional. And you'll see that right here. The parameters it accepts, this function accepts parameters of a string and an optional set ofas well as any delimiters you might want, but they're optional. And you'll see that right here. The parameters it accepts, this function accepts parameters of a string and an optional set of delimiters. Now, you'll see a huge crop here in the sidebar of other functions you can call, and that's kind of overwhelming. But rest assured, many of these you'll never use. For example, this one right here, Levenstein, I've never used it before. Okay. So, you're going to find a handful, maybe a few dozen, maybe 50 that you'll reference. And then the others you can refer to and those isolated instances where it's necessary. Okay. So, don't get overwhelmed. Just learn a few a day, and you'll be good. Okay. So, if we come back, I can get rid of this, and this is looking pretty good. So, now, what about this Boolean we have here, completed? Okay. Why don't we do this? Now, if we switch back to Chrome, did you notice, however, that completed, well, you were trying to Manual Output and Ternary 7:25good. So, now, what about this Boolean we have here, completed? Okay. Why don't we do this? Now, if we switch back to Chrome, did you notice, however, that completed, well, you were trying to echo out a Boolean, and you can't do that. For false, we're not going to see anything. If you set it to true, you might see one. So, this is where you might need a bit more control. Now, you could do this in a number of ways. Rather than filtering through the array, you could do it one at a time. So, for example, you could say, and this is especially good if you want to control what the keys are. So, for example, maybe rather than title, you want it to be the name of the task. Okay. Well, you can write it out manually in this case. Name. And now, we will echo out task title. So, notice here, it's a bit more verbose, but you get a little more control here. Okay. Let's do the rest of them. The due date. Let's copy this. Due date. And that will be due.task title. So, notice here, it's a bit more verbose, but you get a little more control here. Okay. Let's do the rest of them. The due date. Let's copy this. Due date. And that will be due. Next, we have assigned to. So, we'll say, let's do something different. Person responsible. And that will be assigned to. And then finally, completed. Yeah. This is where we need to do something a little different. So, we'll say, why don't we call it status? What is the status of the task? So, if we put it like this, once again, we're not going to get anything. Instead, let's check to see the value of completed. Is it true or is it false? And then proceed. Now, I'm going to show you a new technique called the ternary operator. It looks like this. Check this value and determine if it's truthy or falsy. Now, if it's true, represented by a question mark, why don't we write incomplete? Now, if it's false, we'll say incomplete. Okay. So, you see the basicand determine if it's truthy or falsy. Now, if it's true, represented by a question mark, why don't we write incomplete? Now, if it's false, we'll say incomplete. Okay. So, you see the basic shape that takes? We check a Boolean. Maybe true. And if that is true, then do something. And if it's not true, do something else. This is the ternary operator in effect. And this will make a bit more sense, especially when we talk about conditionals. So, for now, just accept the syntax. May look a little weird to you, but you'll get the hang of it pretty quick. So, now, we're echoing out not true or not false, but some predefined value based upon whether it's true or false. And that looks good. So, in this case, it's incomplete. So, notice that you have a lot of flexibility here. In some cases, you can get away with a for each, where you dynamically build up the HTML. In other situations, you may need quite a bit more control. Remember where we talked aboutflexibility here. In some cases, you can get away with a for each, where you dynamically build up the HTML. In other situations, you may need quite a bit more control. Remember where we talked about adding a green check mark, depending upon whether it's completed? Once again, now, because we've separated it, we can do that really easily. And, in fact, we'll tackle that once we talk about conditionals. Okay. So, that'll do it for this lesson. You now know about strings. You learned about integers, like one or two. You learned about Booleans, like true or false. And you also learned about arrays. So, you're making a lot of good progress. I know this is really hard. Trust me, it's hard for everyone. You're not alone. So, just keep working on it and move on to the next lesson.