Docstoc

PHP basics

Document Sample
PHP basics Powered By Docstoc
					PHP Basics Tutorial 01
  (1) Write a PHP script to Iterate through following Array using for loop?
   $array_ex=array("Apple","Banana","Mango","Orange","Grapes")



   <?php
   $array_ex=array("Apple","Banana","Mango","Orange","Grapes");

   for($sze=0,$i=count($array_ex);$sze<=$i;$sze++)
   {
           echo $array_ex[$sze],"<br>";
   }
   ?>

  (2) Write a PHP script to iterate through following array using while loop?
   $array_ex=array("Apple","Banana","Mango","Orange","Grapes")



   <?php
   $array_ex=array("Apple","Banana","Mango","Orange","Grapes");
   $i=0;
   while($i<count($array_ex))
   {
           echo $array_ex[$i],"<br>";
           $i++;
   }
   ?>



  (3) *
      **
      ***
      ****
      *****
      ******
      *******
      ********
      *********
      **********
Print above output using two nested for loops.

<?php
for($t=0; $t<=10; $t++)
{
        for($i=0; $i<$t+1;$i++)
        {
                echo("*");

        }
        echo "<br>";
}
?>



(4) Write a program to print Square Root of 1 to 100.
 Hint: Use Mathematical functions
 1 =>1
 2=> 1.4142135623731
 .
 .
 100=>10

<?php
for($i=1;$i<=100;$i++)
{
        echo sqrt($i);
        echo "<br>";
}

?>




(5) Write a program to find the maximum value of array using for loop.
 $no=array(34,54,56,78,21,23,45,67,65)

<?php
$no=array(34,54,56,78,21,23,45,67,65);

echo max($no);
     ?>




     (6) Write a program using wile loop to print the elements of the following array

$presidents = array(1 => 'Washington',”Name1”=> 'Adams',
'Jefferson', Name2”=> 'Adams''Madison');




<?php

$presidents = array(1 => "Washington","Name1" => "Adams",
"Jefferson", "Name2" => "Adams","Madison");



$i=0;

while($i<count($presidents))

{

       print_r (array_keys($presidents));

     $i++;

}

?>



Hint:Use array_keys() functions.

     (7)      Write a program to merge following two arrays.

     $array1 = array("color" => "red", 2, 4);
     $array2 = array("a", "b", "color" => "green", "shape" => "trapezo
     id", 4);




     <?php

     $array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" =>
"trapezoid", 4);



$marge = array_merge($array1,$array2);

print_r ($marge);



?>

Hint: Use array functions.

				
DOCUMENT INFO
Shared By:
Categories:
Tags: basics
Stats:
views:7
posted:4/16/2013
language:
pages:5
Description: PHP basics