Q:

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

belongs to collection: PHP Find Output Programs

0

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.

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;
    }
?>

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;
    }
?>

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 . ' ');
        }
    }
?>

Question 4:

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

 

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;
    }
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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. 

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (User-defined functions) ... >>
<< PHP find output programs (User-defined functions) ...