ADVERTISEMENT
ADVERTISEMENT

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.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.