You down with PHP?

Part 3

Robert M Ricci
Geek Culture
3 min readMay 16, 2021

--

Photo by James Harrison on Unsplash

This is my third installment of me journey with PHP. The first post I gave a little history of PHP. We dove into variables, assignment operators, and contacting with the (.) dot operator. You can read that article here. In the second one which you can read here, we dove into functions, both custom and built in. In this one we are going to go over array structure. I will give example on how to create them, as well as how to modify them and also the few differnt ways to display them.

As a referesher an array is just a collection of data, where each piece holds a numerical index number. These indexes start at zero and end at the last element. In PHP a standard array is known as an ordered array. PHP has two differnt ways of creating arrays. One uses the array(), keyword, and hte other uses bracket notation[]. Check out the examples below.

To access an element in an array, all you need to do is call its index. So for the first array, I called index zero and “First” was returned because zero in the first index. In the second array I call two and “Sixth” was returned because it was the last index, even though their are three elements, the indexng starts at zero.

Now how can we modify an array? Well, their are a few differnt ways to both add and subtract elements from arrays. The first two we wil go over is array_pop() which removes the last element form an array and array_shift() which removes the first element from an array.

On the inverse of that their are also ways to add elements to arrays. We have array_push() that adds elements to the end of an array and array_unshift() that adds to the beginning of an array.

PHP has another form of array which is associative array. Which aould be similiar to a hash in Ruby. The main distinction which these are the key=>value dynamic, whese instead of an index you have a keyword representing the value. Here is an example.

The last thing I want to go over is how to combine arrays. You would use what is known as the union operator(+). The thing to note here is that the second array is always append, meaning that it goes to the end. So becareful you orer if you want a specific order.

That’s arrays in a nutshell. I would highly suggest digging into PHP’s documentation. It has a lot of great info, with a ton more detail that I can’t cram in here without making this super long.

--

--

Robert M Ricci
Geek Culture

Full Stack Developer Ruby and Javascript. Recent grad of the Flatiron School.