All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Declare Array In PHP

Last Updated : Mar 11, 2024

How To Declare Array In PHP

In this tutorial we will show you the solution of how to declare array in PHP, as we know array is used for when we handle more number of values.

For handling array first we need to declare them so here we declared array in three types with example.

In our example we declared empty array, array with values initialization and associative array with values then we printed array values.

Step By Step Guide On How To Declare Array In PHP :-

Here we first declared empty array and it’s variable name is ‘$arr’ then we declared another array with numeric values initialization this array name is ‘$arr1’ and initialized numeric values are ‘1,3,5,9’.

We can initialize array with string values also then finally we declared associative array or we can say multidimensional array with values initialization array named as ‘$arr2’ and values are ‘"Name"=>"Prawin","Age"=>"25","Occupation"=>"MD"’.

<?php
$arr=array();
$arr1=array(1,3,5,9);
$arr2=array("Name"=>"Prawin","Age"=>"25","Occupation"=>"MD");
echo "First Declared Array Values<br>";
for($i=0;$i<count($arr1);$i++){
    echo $arr1[$i],"<br>";
}
echo "Second Declared Array Values<br>";
echo "Name is ",$arr2["Name"];
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. For declare an array we need to know syntax of array declaration in php (i.e) $array_name=array(value1,value2,..).
  4. Here first we declared empty array then its name ‘$arr’ and it had no initialized values so it is empty array.
  5. We created another array ‘$arr1’ with initialized numeric values ‘(1,3,5,9)’ and lastly the associative array declared ‘$arr2’ with values it had combination of ‘key and values’ ‘"Name"=>"Prawin","Age"=>"25","Occupassion"=>"MD"’.
  6. Instead of numeric values we can declare an array with string values (i.e)$arr=array(“abi”,”akil”).
  7. Using echo() we printed message ‘First Declared Array Values’ on webpage then using for loop we iterated $arr1 values then printed one by one.
  8. Then another message printed like above seen and in associative array we printed key ‘Name’ value only. We can also print key of ‘Age or Occupation’ values or using print_r we can print whole associative array values on webpage.
  9. We can create those array with any other values as your wish here shown example for your reference only try to explore different values but concept will same.

Conclusion :-

In conclusion we are able to know how to declare an array with or without initialized value in php.

First we need to start our xampp server then we load this program on browser we can see first message ‘First Declared Array Values’ for user clarification then we printed $arr1 all values then another message like associative array heading and associative array key ‘Name’ value ‘Prawin’ if you want to print age value of associative array that is same as how we print key ‘Name’ we just need to change key name (i.e)$arr2[Age] so it will display result ‘25’ and for Occupation value also same.

I hope this tutorial on how to declare array in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪