PHP Basics

Arrays

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Arrays" 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 how to create arrays in PHP. The array() constructor accepts the list of items to build the array. Square bracket notation is also supported. Keys can be provided to create an associative array like a dictionary or map. The global function count() will return the length of an array.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Arrays" Lesson

[00:00:00]
>> Maximiliano Firtman: Arrays, let's create a new one for array and collections. Arrays are actually pretty simple, you just create a variable and you create a list. And the default constructor for an array is the array function. And then you can say the list of number, okay? So I can create something like this, there's an array.

[00:00:22]
By the way, why is not coloring my PHP code? Everything is black and white.
>> Speaker 2: You don't have a tag.
>> Maximiliano Firtman: I don't have the PHP tag. Remember, you will forget, okay? So I need a PHP tag. Maybe I don't need to close it, but now at least I need to open, okay?

[00:00:40]
Another way, and you would probably like this is, from modern PHP, it wasn't there in the 90s. You can create, for example, a list of countries and we can use kind of, Jason syntax so we can use scrap brackets to create a little, right? So I will start from Argentina because it's a, right?

[00:00:59]
Not because it's the best country, so, Argentina, Brazil, Canada, Denmark, whatever, both are the same. I'm just creating the same kind of array, okay? So it's sure syntax to the array constructor. So also arrays, this is interesting. Arrays can always contain keys, it isn't an array without keys, but I can create a key.

[00:01:26]
What's a key? Anything. It can be a string, it can be another number, so for example I can say for this list, this is the ID, and then I can use this. The fat arrow to express key value. So and this is the limit, whatever. So I have a list and the first element has a key, the second element has no key.

[00:01:55]
You can match and mix so when you add a key it's called an associative array in PHP. This is similar to a dictionary, a hash table, a map on other platforms. Here, everything is an array. If you don't define a key, you will have an index, as you're expecting.

[00:02:17]
Okay, so I can say countries, it will give me Canada as you are expect so that will work. But also I can get from the list, I can say subzero or I can say sub id, okay? Similar how in JavaScript you can browse an option because it has a key.

[00:02:44]
I can use that key there as well, make sense? It's actually simple, I mean, it's not a big deal. That's an advantage of collections or arrays. It's actually pretty simply PHP. And by the way, how do you think or how would you think that getting the length of the array should work?

[00:03:08]
So if I want to render this length first, we haven't talked about objects, but should it be something like .size, .length. Well, no, this is not an object. By default, PHP is not object-oriented. So, nothing is an object unless it is an object. And we are not talking about objects yet.

[00:03:32]
So, we don't have .syntax, but that's for later. So, that's not an option. So, we work with functions, global functions. Okay, so does anyone know? I'm sure of it. So you just count it's a global function. I think you mentioned, well, strings. Should I use counts for strings to count the characters?

[00:04:00]
If I have a string. Probably not. Probably not you say, it's actually strlen. So strlen string length, it's actually how you get the length of a string. Again, you will find this lack of consistency many times in PHP, okay? It's not the same function, it's a different function.

[00:04:25]
Sometimes it's using a different naming guideline. Okay, you will get used with time, mostly with the function that you use on a daily basis.

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