So now we're going to talk about array functions. PHP started out as a non-object programming language so it tends to have a bunch of functions that are named in a way to group them together. So if we take a look at some of the array functions. We'll see that they sort of have array_ and that's just a way in the global name space. You'll see that string functions have string_ and so that's a way to do it. And we do a lot of manipulation of arrays through functions, where we pass the array in as a parameter. And that's because these are not object-oriented. We'll talk about OO later, but these arrays are kind of from PHP 1, 2, 3, 4. PHP 5 is where object-orienting came in. And so these are kind of antiques within PHP. So we have a lot of things. But at the same time, there's a lot of really powerful things. Because like I said, programmers build data structures, and then they take this array and then merge it with this array or this array. Compute the differences between these things, etc. Find the position of this key inside of an array. So there's a lot of things because that becomes the way that you express your program and allows your programs to be relatively small. Because you have these sort of these operations on arrays. So here's a couple that we'll talk about. You can ask if key exists in array. This is kind of a cleaner way to do it. But the way everybody does it is this isset function, ($ar['key']). And for those of you who know the programming languages, this makes me quizzical how this actually works inside of PHP. Because if you say ar['key'] and a key doesn't exist, $ar['x'] and x is not there, this becomes a non-fatal error. So what we always ask is want to use one of those two things to see if the key is in there. And we tend to use this one all the time because it's more succinct. But that doesn't cause a non-fatal error. Neither of these cause a non-fatal error. If you just access a non-existant key, it does cause a non-fatal error. We already saw the count which tells you how many things are in there. One of the things that's really cool about PHP is that you can have variables that are what's called mixed type. They might be a string or they might be array of strings. And you can write a function that will take an array of strings or a string but you have to know if it's an array or not. So this is a logical test, is that an array or not? Sorting, awesome, because arrays maintains the order that you put them in. So there's a couple ways to sort, which we'll talk about. And you can even sort of randomly shuffle them. Make an array of playing cards, and then just shuffle them. And it randomly shuffles them, gives you a different order. Okay, so let's ask a few questions. Just because of the fact that you can't try to dereference an array, $za['x'], in this case, will blow up because x isn't there, right? And so I got name and course in there, and so I have to somehow ask, does the key exist? Does the course key exist in the za array, true or false? And in a sane world we would do that, but we do that way too often because we don't have a get operation the way we do in Python. And actually this gets different in PHP 7, and so you're probably using PHP 7 and there's a cleaner way to do this in PHP 7. But the compatible way of doing this with PHP 5 and earlier is to use the ternary operator. And so this is the one place that, even though the ternary operator is a little crazy. Remember ternary operator, which is a question and the true part and the false part. And so this basically is an expression, you can put parentheses around it right there. This is going to ask isset, which is going to return us a true or a false. In this case it's going to return true because name is in the array. And so it's going to print the true part, so it's going to say name is set, so that prints that out. Now if we do it again, this is going to ask, isset ($za ['addr']), and that is going to be false. And so it's going to pick the false side, and so it's going to take and print out addr is not set. And so again, this is the ternary operator, question mark, colon. That basically is going to take the true or the false, depending on the evaluation. So it turns out this we do all the time. And I apologize, you'll see this in my code for the rest of this class all the time. Someday we will use PHP 7. PHP 7 has a more elegant way of doing this, with that's called the null coalescing operator. It really is a operator that sort of also suppresses that non-fatal error. And so the null coalescing operator is the ??. This is a PHP 7.0 only. So you probably are installing PHP for the first time. And so you'll have PHP 7, so you don't have to use this ternary operator. You can achieve the same thing by saying, username, $_GET is an array we'll talk about in a second, but it is just an array. Give me what's in the key user or nobody if it's empty. So it's really, this comes across or that comes across, and this is kind of like a default. I like this, I wish it was in there all along. Smiley face, I like that, I wish it was in there all along, okay? And so that's the null coalescing operator. So this is a lot prettier. We can link this array with two things, if it's greater than 7 we can say if it's there put the not found in. If it's not there put the not found in, in this case the name is going to go and in this case not found is going to go. And then the equivalent is this is going to be true or false and then the true part happens and the false part happens. This works in PHP 7 as well. And so this is why you'll see my sample code at least for the next couple of years until we're at PHP 8 or 9. I don't like to write code that can't go back to PHP 5. PHP 6 never happened. PHP 6 is the release that doesn't exist so there was 5 and then 6 no one liked 6 and so they got rid of it and made it 7. So the release right before 7 is PHP 5. Someday we'll have 8 and 9 and then everyone will be upgraded. I'm really conservative so I still use this, but you can use this because you're starting on PHP 7. But when you go to work you'll probably be in PHP 5, or they'll tell you that you gotta stay compatible. So I want you to know both of them but I know that the future is bright and I'm not going to have to use the ternary operator ever again which will make me very happy. We've already talked about count. Just tells you how many things are in the array. is_array, is it true or false? Yes it is an array, very useful. Mostly useful if you're building functions and taking mixed parameters. And you'll see, when you look at the PHP function specifications, they'll say that the first parameter's mixed, which means it could be an array, it could be a string, it could be something. It's really easy to write code that accepts strings or arrays of strings, which is quite nice. So let's talk about sorting. So the funny thing is, probably the worst sort that you have in all of PHP is the one name sort. Because it doesn't sort key value arrays very well. It does sort numeric arrays well. So it re-does the arrays based on the values, not the keys, but it throws away the keys. So this resorted them. So it sorted the things into the right order but then it wiped these things out. If you're sorting a linear array this is fine. But if you're sorting a key value array it is not a very fine thing to do. And so you can do what's called a ksort, which is respecting sort by the keys, course name topic. Or you can do asort which sorts by the values but keeps the keys in place. Probably this is, well it depends what you're trying to do, but asort is probably what you want to do. Although when I'm printing stuff out, I tend to like doing a ksort so that I can see the keys. So if I'm looking, sometimes there's like 40 things in here. So you want to find the things, so I tend to sort by keys, just because, is age in there, or whatever it's looking for. And so ksort and asort, I don't know why this is asort, I would call this vsort, because it's like keys and values. Why didn't they call it vsort? I don't know, they'll need to change that. I can complain about a lot of stuff but I won't complain about that, asort is awesome. That might be it! A stands for awesome, it's the awesome sort. There's the sort that messes up the keys and then awesome sort that doesn't mess up the keys. And then ksort that sorts by keys. Maybe that's what's going on here, who knows? Someone can tell me in the comments if that's what's going on. So another thing that we do in many programming languages is string processing. And so there is this capability in PHP called explode. What it takes as its first parameter is the split. The character to split on, the string to do the splitting, and then you get back an array. So it just goes chop, chop, chop, chop, chop, chop. And so then I get back, in temp, I get an array that is each word of that in an array. So you can say this is the 0, 1, 2, 3, 4, 5. So the fifth word, [5], is seven. Very nice, you can make this be a comma, you can make it be a colon, semi-colon, whatever it is that you're trying to split on. And so that's a great way to parse data. So up next we're going to connect how arrays work with the request response cycle.