From the course: PHP for WordPress (2020)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Using arrays

Using arrays

- [Instructor] Arrays are a bit different from the rest of our variables. Arrays act as key-value pairs that essentially function as a set of variables. So let's say we want a collection of colors like we have on the screen here. We could store each color in a variable where color1 gets red, color2 gets green, et cetera but that can get cumbersome and hard to manage and depending on the size of the data, inefficient for PHP to run. So instead, we would use an array. We can create a single variable called colors and then use the array keyword. When we add an array, we would use the keyword array and then an opening parentheses. Then we would add each color in a comma-separated list. So we would write single quote red and single quote comma green comma blue comma yellow. And then the closing parentheses and the semicolon. Now, we can reference each element in the array based on an index. But to better…

Contents