Basic Concept Of Arrays In PHP

If you are a PHP web developer and if you want to build a PHP program properly, ie, in clean way, then, you may need to have a basic concept of the arrays, which can be used in PHP, since, it can reduce the work load of the PHP project to greater extent due to its flexibility and easiness. So, you might want to know about the basic concepts of arrays used is PHP, right? Well, here, we will provide you with the basic concept of arrays used in PHP. The basic example of PHP code with the concept of arrays in PHP code is provided below with their respective comments:

<!DOCTYPE html>
<html>
<head>
<title>Arrays</title>
</head>
<body>
<?php
$numbers = array(4, 8, 12, 16, 20, 24);
echo $numbers[1]; // displays the content of the array with position '1', ie (8), because, array always starts with the index 0.
echo "<br />";
echo $numbers[0]; // displays the content of the array with position '0', ie (4)
echo "<br />";
?>

<?php $mixed = array(5, "cat", "dog", array("A", "B", "C")); ?>
<?php echo $mixed[2]; ?> <br />
<?php // echo $mixed[3]; ?> <br /><!-- it displays array, because it has a content of an array element inside it declared as array("A", "B", "C") -->
<pre>
<?php echo print_r($mixed); ?>
</pre>

<?php echo $mixed[3][1]; ?> <br /><!-- it displays the contents of the array elements contained inside the array index of [3][1], ie the index here applied is for array("A", "B", "C") -->

<?php $mixed[2] = "rat"; ?> <!-- it overrides the content of array which is indexed in '2' with the new index of '2' as 'rat' -->
<?php $mixed[4] = "cow"; ?> <!-- it adds the new index in the array with the index of '4' with the value of 'cow', so, now the index of array is upto 4 -->
<?php $mixed[] = "buffalo"; ?> <!-- it adds the new index in an array with the index number choosen as the last one, ie it is inserted in an array with the value of unknown index, if the array has '2' indexes, then this value is inserted in the index number of '3' and so on -->
<pre>
<?php echo print_r($mixed); ?>
</pre>
<?php
// only available for php 5.4 or later
// this type of indexing in an aray is direct indexing and it can't be used below the php version of 5.4, it is same like the other one as done above
$array = [1, 2, 3, 4];
?>

</body>
</html>
arrays-in-php

The above image is the result of the above PHP program code provided. Here, in the above output, we can see that the first output is 8, since arrays in PHP always starts with 0, not 1, and in the second case, we have passed the array position to be of 0, hence, the result on this is 4, since, 4 is in the first array, ie, list of array. Similarly, you can see the output of the above program too as in the provided image above and also you can learn more via the comments added in the above provided PHP program too.

If you have read the above code carefully and done it by yourself in your development environment, then, we can assume that you now have the basic concept of arrays in PHP, about how to use it and its basic syntax.

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.