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

Find the output of PHP programs | User-defined functions | Set 3: 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
    $ARR = array(
        10,
        55,
        34,
        65,
        21,
        05
    );

    FUN($ARR);
    
    function fun($A)
    {
        $VAR = $A . at(0);
        foreach ($A as $V)
        {
            if ($VAR < $V) $VAR = $V;
        }
        echo $VAR;
    }
?>

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function at() 
in /home/main.php:15
Stack trace:
#0 /home/main.php(11): fun(Array)
#1 {main}
  thrown in /home/main.php on line 15

Explanation:

The above program will generate syntax error, because of the below statement.

$VAR = $A.at(0);

Here, we used at(), which is not a built-in function in PHP.

Question 2:

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

Output:

PHP Parse error:  syntax error, unexpected '*', 
expecting variable (T_VARIABLE) in /home/main.php on line 5

Explanation:

The above program will generate syntax error because we created function fun() with parameter $VAL, which looks like a pointer. But PHP does not support pointers.

Question 3:

<?php
    FUN(add(printf("AB") , printf("CD")));
    
    function fun(int $VAL)
    {
        for ($i = 1;$i <= 10;$i = $i + 1)
        {
            echo ($i * $VAL . ' ');
        }
    }
    
    function add($A, $B) 
        return ($A + $B);
?>

Output:

PHP Parse error:  syntax error, unexpected 'return' (T_RETURN), 
expecting '{' in /home/main.php on line 13

Explanation:

The above program will generate the compile-time error because here we defined a  function add() without curly brasses, it is mandatory to use curly brasses in the definition of function in PHP.

Question 4:

<?php
    FUN(add(printf("AB") , printf("CD")));
    
    function fun(int $VAL)
    {
        for ($i = 1;$i <= 10;$i = $i + 1)
        {
            echo ($i * $VAL . ' ');
        }
    }
    
    function add($A, $B)
    {
        return ($A + $B);
    }
?>

Output:

ABCD4 8 12 16 20 24 28 32 36 40

Explanation:

In the above program we defined two functions fun() and add().

The add() function is used to return the sum of two specified numbers, and fun() function is used to print the table of a specified number.

Now look to the function call.

FUN(add(printf("AB"),printf("CD")));

In the above function call FUN(), the printf("AB") will print "AB" and return 2, similarly printf("AB") will print "AB" and return 2. Then the final function call looks like:

FUN(add(2,2));
FUN(4);

The table of 4 will be printed on the webpage.

Question 5:

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

Output:

2 4 6 8 10 12 14 16 18 20

Explanation:

In the above program, function fun() will print the table of the specified number. The function log(10) will return 2. Then the function call will be FUN(2). Then the table of 2 will be printed on the webpage.





Comments and Discussions!

Load comments ↻





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