0% found this document useful (0 votes)
108 views5 pages

PHP Basics Tutorial 01: ?PHP $STR 'This Is A Test.' $first $STR

The document provides examples and exercises on PHP basics including: 1. Using for and while loops to iterate through arrays 2. Explaining output of PHP scripts performing string operations 3. Using nested for loops to print a pattern 4. Calculating absolute value, square root, maximum value in an array 5. Summarizing student data from a multi-dimensional array 6. Using while loop to print array elements 7. Merging two arrays
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views5 pages

PHP Basics Tutorial 01: ?PHP $STR 'This Is A Test.' $first $STR

The document provides examples and exercises on PHP basics including: 1. Using for and while loops to iterate through arrays 2. Explaining output of PHP scripts performing string operations 3. Using nested for loops to print a pattern 4. Calculating absolute value, square root, maximum value in an array 5. Summarizing student data from a multi-dimensional array 6. Using while loop to print array elements 7. Merging two arrays
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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")

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

(3) Execute following programs and explain outputs?


<?php $str = 'This is a test.'; $first = $str[0];

echo $first.'<br>';

$third = $str[2];

echo $third.'<br>';

$str = 'This is still a test.'; $last = $str[strlen($str)-1];

echo $last.'<br>';

$str = 'Look at the sea'; $str[strlen($str)-1] = 'e';

echo $str.'<br>';

$third = $str{2};

echo $third .'<br>'; ?>

<?php $foo = 1 + "10.5"; echo $foo.'<br>'; $foo = 1 + "-1.3e3"; echo $foo.'<br>'; $foo = 1 + "-1.3E-3"; echo $foo.'<br>'; $foo = 1 + "bob-1.3e3"; echo $foo.'<br>'; $foo = 1 + "bob3"; echo $foo.'<br>'; $foo = 1 + "10 Small Pigs"; echo $foo.'<br>'; $foo = 4 + "10.2 Little Piggies"; echo $foo.'<br>'; $foo = "10.0 pigs " + 1; echo $foo.'<br>'; $foo = "10.0 pigs " + 1.0; echo $foo.'<br>'; ?>

(4) * ** *** **** ***** ****** *******

******** ********* ********** Print above output using two nested for loops. (5) Write a program to get the absolute value of an integer number. Hint: Use Mathematical functions (6) Write a program to print Square Root of 1 to 100. Hint: Use Mathematical functions 1 =>1 2=> 1.4142135623731 . . 100=>10

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

(8) Following is a multidimensional array $students =array( array( "name"=>"saman", "Subject1"=>45, "Subject2"=>35, "Subject3"=>56), array( "name"=>"Ravindra","Subject1"=>66, "Subject2"=>75, "Subject3"=>56), array( "name"=>"Bhagya", "Subject1"=>95, "Subject2"=>65, "Subject3"=>56), array( "name"=>"Nishanthi", "Subject1"=>87, "Subject2"=>66, "Subject3"=>56),

array( "name"=>"Thilini", "Subject1"=>77, "Subject2"=>97, "Subject3"=>56));

Above array contains student names and three subject marks. You need to calculate total and average for the each student and print in a following table.

Name

Subject1

Subject2

Subject3

Total

Average

(9) Write a program using wile loop to print the elements of the following array $presidents = array(1 => 'Washington',Name1=> 'Adams', 'Jefferson', Name2=> 'Adams, 'Madison'); Hint:Use array_keys() functions. (10) Write a program to merge following two arrays.

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

You might also like