Tuesday 14 January 2014

PHP crash course -3

IN this section we will discuss about array in PHP . which is quit similar to list in Python .

What is an array : A variable is storage area holding a number or text . But the problem is variable can hold only one value at a time .
So an array is a special variable ,which can store multiple values in single variable .  


There are three type of  array in PHP : 
                * Numeric array : an array with numeric index .
                *Associative array : An array where each ID key is associative with a value .
                * Multidimensional Array :  An array containing one or more array .

Numeric array :

There are two method to create a numeric array .
Creating array at a time .
 Adding element one by one .

In this example we can see both method ...

 <?php  
 // Array in PHP is slightly different from c lang  
 //But it inspire by Python  
 // Numeric Array :::::::::::::::::  
 $cars=array("saab","volvo","BMW","Toyata");  //way one
 echo "$cars[0] <br /> ";  
 echo "$cars[1]<br />";  //Printing volues one bye one
 echo "$cars[2]<br />";  
 echo "$cars[3]<br /><br />";  
 $cars[4]="farari"; // Adding New Item  bye way 2
 echo "<b>$cars[4]</b>";  
 //<br /> stands for line Break : it is HTML syntax  
 // and <b></b> stand for bold in HTML tag  
 ?>  

And its output is like this


Related post : Basic funda of Internet Surfing for Person wan't to be hacker

Associative array : In this each ID key associated with a value .With associative array we can use the values as keys and assign value to them .

In this Example we use an array to assign ages to different persons :

 <?php  
 //associative array   
 $ages=array("Peter"=>20,"sudhanshu"=>21,"shaan"=>30,);  //way 1
 echo "Peter is ".$ages["Peter"]." Year old <br />";  
 echo "sudhanshu is ".$ages["sudhanshu"]." Year old <br />";  
 echo "shaan is ".$ages["shaan"]." Year old <br /><br />";  
 $ages['Shakti']="18"; // adding New data manually  
 echo "Shakti is ".$ages["Shakti"]." Year old <br />";  
 ?>  
Output of this : first see program and analyse is 


Multidimensional Array : In Multidimensional array ,each element in the main array can also be an array.And each element in the sub-array can be an array and so on.
 <?php  
 // Multidimensional array .......  
 // Ex => A two-dimensional array:  
 $cars = array  
  (  
  array("Volvo",100,96),  
  array("BMW",60,59),  
  array("Toyota",110,100)  
  ); // Creating Multidimensional array 
// Printing Array
 echo $cars[0][0]." : Ordered: ".$cars[0][1].". Sold: ".$cars[0][2]."<br>";  
 echo $cars[1][0]." : Ordered: ".$cars[1][1].". Sold: ".$cars[1][2]."<br>";  
 echo $cars[2][0]." : Ordered: ".$cars[2][1].". Sold: ".$cars[2][2]."<br>";  
 ?>  

OUTPUT of this

This is all very similar to simple concept of ARRAY In PyThon . But Its all other Basic property of  C language.

In Next section we will see Very important feature of  PHP ie $_GET,$_POST and $_REQUEST function.

.PHP crash course -2 
PHP crash course -1

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets