Friday, 2 May 2014

Examples of PHP Program Using Array


Array can be intialized in three ways in PHP.

1st Way :   $col=array("blue","red","green","white","pink");

2nd Way :   $col[ ]="blue";
                    $col[ ]="red";
                   $col[ ]="green";
                   $col[ ]="white";
                   $col[ ]="pink";

3rd Way :   $col[0]="blue";
                   $col[1]="red";
                   $col[2]="green";
                   $col[3]="white";
                   $col[4]="pink";

Here you can find the Example please click below
Example


TO Find the Sum Of An Array Using PHP

<?php
$sum=0;
$arr=array(1,2,3,4,5);

for($i=0;$i<count($arr);$i++)
{
$sum=$sum+$arr[$i];
}
echo "Sum Of Given Array is:" .$sum;
?>


To Find The Sum of Even And Odd Numbers

<?php 
$arr=array(10,11,12,13,14,15);
for($i=0;$i<count($arr);$i++)
{
 if($arr[$i]%2==0)
  {
@$even=$even+$arr[$i];
  }
 else
  {
  @$odd=$odd+$arr[$i];
  }
}
echo "Sum of even=".$even."<br/>";
echo "Sum of odd=".$odd;
?>


No comments:

Post a Comment

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...