PHP find output programs (User-defined functions) | set 2

Find the output of PHP programs | User-defined functions | Set 2: Enhance the knowledge of PHP User-defined functions concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 22, 2021

Question 1:

<?php
    $NUM = power(5, 0) * power(5, 1);
    
    $RES = fun($NUM);
    printf("RESULT: %d", $RES);
    
    function fun(int $VAL)
    {
        $F = 1;
    
        for ($i = $VAL;$i > 1;$i--)
        {
            $F = $F * $i;
        }
        return $F;
    }
?>

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function power() 
in /home/main.php:2
Stack trace:
#0 {main}
  thrown in /home/main.php on line 2

Explanation:

The above program will generate syntax error because power() is not a built-in function.

Question 2:

<?php
    $NUM = pow(5, 0) * pow(5, 1);
    $RES = fun($NUM);
    printf("RESULT: %d", $RES);
    
    function fun(int $VAL)
    {
        $F = 1;
        for ($i = $VAL;$i > 1;$i--)
        {
            $F = $F * $i;
        }
        return $F;
    }
?>

Output:

RESULT: 120

Explanation:

In the above program, we created a variable $NUM, which is initialized with the below expression.

$NUM = pow(5,0)*pow(5,1);    
$NUM = 1*5;
$NUM = 5;

Now coming to the definition of function fun(). Here we pass value 5. In the body of function fun(), we declared a local variable $F with initial value 1. Here the value of $i is initialized with 5 and the loop will execute the value $i is greater than 1.

Now look,

$i = 5, $F = 1 and condition is true
    $F = $F * $i;
    $F = 5 * 1;
    $F = 5;

After decrement of $i value.
$i = 4, $F = 5 and condition is true
    $F = $F * $i;
    $F = 5 * 4;
    $F = 20;

After decrement of $i value.
$i = 3, $F = 20 and condition is true
    $F = $F * $i;
    $F = 20 * 3;
    $F = 60;

After decrement of $i value.
$i = 2, $F = 60 and condition is true
    $F = $F * $i;
    $F = 60 * 2;
    $F = 120;

After decrement of $i value. 
$I=1 then condition get false and loop will terminate.

Question 3:

<?php
    $NUM = pow(5, 0) * pow(5, 1);
    
    FUN($NUM);
    
    function fun(int $VAL)
    {
        for ($i = 1;$i <= 10;$i = $i + 1)
        {
            echo ($i * $VAL . ' ');
        }
    }
?>

Output:

5 10 15 20 25 30 35 40 45 50

Explanation:

In the above program, we created a variable $NUM, which is initialized with the below expression.

$NUM = pow(5,0)*pow(5,1);    
$NUM = 1*5;
$NUM = 5;

Now coming to the definition of function fun(). Here we pass value 5. In the body of function fun(), Here value of $i is initialized with 1, and the loop will execute the value $i is less than equal to 1.

Then the loop will print the table of 5, in every iteration value of $i will multiply with 5 and print the table on the webpage.

Question 4:

<?php
    $ARR = array(
        "RED",
        "GREEN",
        "BLUE"
    );
    
    FUN($ARR);
    
    function fun($A)
    {
        foreach ($A as $V)
        {
            echo $V . ' ';
        }
    }
?>

Output:

RED GREEN BLUE

Explanation:

In the above program, we created an array that contains values "RED", "GREEN", "BLUE". Here, we pass the array into user define function fun(), and in the function body, we accessed elements of the array using a foreach loop and print them on the webpage.

Question 5:

<?php
    $ARR = array(
        10,
        55,
        34,
        65,
        21,
        05
    );
    
    FUN($ARR);
    
    function fun($A)
    {
        $VAR = $A[0];
        foreach ($A as $V)
        {
            if ($VAR < $V) $VAR = $V;
        }
        echo $VAR;
    }
?>

Output:

65

Explanation:

The above program will print 65 on the web page. In the above program, we created an array that contains integer numbers, and we created a user-define function with array as an argument.

In the function body, we created a local variable $VAR initialized with the first element of the array. Then we access the elements of the array one by one using the foreach loop. Here, $VAR is compared with each element of the array, if $VAR is less than a specified element swap, then we copied the large value to the $VAR. At the end, $VAR will contain the largest element of the array.

Then 65 will be printed on the webpage.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.