What are Arrays in PHP?

  1. Today we will be learning Arrays in PHP. With the help of Arrays, we can store multiple types of data in one container. There are different types of Arrays.
  2. In variables as you know we can store data but with a limitation of only one single data or value.
  3. I would say Arrays are variables with Superpowers! It’s the number 1 container in programming.
  4. Let’s open our code editor and start with a blank page – Open HTML, head, body, and PHP tags.
  5. Next step – write a variable. Example – $numberList
  6. To make this variable a array simply apply a array function i.e.  $numberList = array(); or $numberList = []; syntax array() or []
  7. Now simply try storing different values e.g. $numberList = array(25,264,52,’99’,348, ‘<h1>Hello</h1>’);
  8. Now carefully note that each value in the array is called index. And an index in the array starts with 0 which means in our example 25 is index 0, 264 is index 1, and so on.
  9. To see the structure of the array. There is a built-in function in PHP to see the structure. Try the “print_r” function to check the structure of the array
  10. Next how do we display array data? Answer – example echo $numberList [0];
  11. With this, you will be able to see value 25 displayed.
  12. Try changing the index value to see the displayed value change.
  13. Hope you had fun learning. Thank you for your time.

Leave a Reply

Your email address will not be published. Required fields are marked *