0% found this document useful (0 votes)
15 views

Loop & Function Problems

The document contains examples of using different PHP loop structures like for, while, do-while loops to iterate over arrays and print values. It also contains examples of using functions to perform tasks like calculating lengths, extracting substrings and calculating totals with tax.

Uploaded by

202180301
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Loop & Function Problems

The document contains examples of using different PHP loop structures like for, while, do-while loops to iterate over arrays and print values. It also contains examples of using functions to perform tasks like calculating lengths, extracting substrings and calculating totals with tax.

Uploaded by

202180301
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Loop & Function Samples

1. Use for loop to print numbers 10 8 6 4 2

for ($x=10; $x>=2; $x-=2){


echo $x . " ";

2. Prompt the user to enter the start and ending numbers and then prints the numbers from the start to the end.

$num1=readline("Enter the start number: ");


$num2=readline("Enter the ending number: ");

for ($i=$num1; $i <= $num2; $i++) {


echo $i;
}

3. Use for loop and array count() function to display the elements of the array $fruits.

$fruits = ['Apple','Banana','Cherry','Dates'];

for ($i=0; $i < count($fruits); $i++) {


echo $fruits[$i].' ';
}

4. Use while loop to print numbers 10 8 6 4 2.

$x = 10;
while ( $x >=2) {
echo "Number:" . $x . "\n";
$x-=2;
}

5. Trace the output.

$x = 3;
$y = 7;

while ($x <= 10) {


echo "Current x: $x | Current y: $y";

// Distracting expression inside the loop


$distractingExpression = $x * $y + 2;
echo " | Distracting expression: $distractingExpression\n";

$x += 2;
$y -= 1;
}

6. Trace the output.

$x = 5;
$y = 2;

while ($y < 8) {


echo "Current x: $x | Current y: $y";

// Distracting expression inside the loop


$distractingExpression = ($x * 3) + ($y / 2);
echo " | Distracting expression: $distractingExpression\n";

$x -= 1;
$y += 2;
}
7. Print all even numbers from 2 to 20 using a do while loop.

$a = 2;
do {
if ($a %2==0) {
echo $a . ' ';
}
$a++;
} while ($a <= 20);

8. Calculate and print the sum of squares of numbers from 1 to 5 using a do-while loop.

$x= 1;
$sum= 0;

do{
$x1= $x ** 2;
$x++;
$sum = $sum + $x1;
echo $x1. " ";
}while($x <= 5);

echo 'Sum of squares: ' . $sum;

9. Given an array of numbers, calculate and print the sum of all elements using a foreach loop.

$numbers = [10, 15, 23, 11, 23, 44];


$sum = 0;

foreach ($numbers as $x) {


$sum += $x;
}

echo "The sum is: ". $sum;

10. Given an array, square each element and print the modified array using a foreach loop.

$numbers = [1, 5, 23, 21, 32, 45];

$numberz = [];

foreach ($numbers as $a){


array_push($numberz,$a**2);
}
print_r($numberz);

11. Given an associative array of prices, calculate and print the total cost using a foreach loop.

$prices = array(
'Apple' => 1.25,
'Banana' => 0.75,
'Cherry' => 2.50,
'Orange' => 1.00,
'Grapes' => 3.00
);
$sum=0;
foreach($prices as $key => $value){

$sum = $sum + $value;

}
echo "Sum: ". $sum;

12. Given an array of numbers, create a new array containing only the odd numbers using a foreach loop.
$numbers = [5, 10, 15, 20, 25];
$array = [];
foreach ($numbers as $odd) {
if ($odd % 2 == 1) {
array_push($array,$odd);
}
}
print_r($array);

13. Create a function to calculate and print the length of a string.

function calculate($ediwow){
return strlen($ediwow);
}

14. Write a PHP program that defines a function extractSubstring($str, $start, $length) which takes a string $str, a starting position
$start, and a length $length. The function should return the substring starting from the specified position and having the
specified length.

Sample output:
Enter a string: The quick brown fox jumps over the lazy dog.
Enter the starting position: 4
Enter the length of the substring: 10
Substring: quick brown

function extractSubstring($str, $start, $length){


return substr($str, $start, $length);
}

$sentence = readline("Enter a string: ");


$s = readline("Enter the starting position: ");
$l = readline("Enter the length of the substring: ");
echo 'Substring: ' . extractSubstring($sentence, $s, $l);

15. Create a function that takes an amount and a tax rate as parameters and returns the total amount including tax. The tax should
be calculated as a percentage of the original amount.

Original Amount: 100


Tax Rate: 10
Total Amount with Tax: 110

function tax($amount, $tax_rate){


$percentage = $amount * ($tax_rate/100);
return $percentage + $amount;
}

You might also like