Q:

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

belongs to collection: PHP Find Output Programs

0

Find the output of PHP programs | User-defined functions | Set 1: Enhance the knowledge of PHP User-defined functions concepts by solving and finding the output of some PHP programs.

Question 1:

<?php
    function fun()
    {
        printf("Hello World");
    }
    echo ("Hello India");
?>

Question 2:

<?php
    fun();
    
    function fun()
    {
        printf("Hello World<br>");
    }
    
    echo ("Hello India");
?>

Question 3:

<?php
    $A = 5;
    $B = 10;
    
    fun($A, $B);
    printf("%d, %d", $A, $B);
    
    function fun(int $A, int $B)
    {
        $A = 20;
        $B = 30;
    }
?>

Question 4:

<?php
    $A = 5;
    $B = 10;
    
    fun(&$A, &$B);
    printf("%d, %d", $A, $B);
    
    function fun(int $A, int $B)
    {
        $A = 20;
        $B = 30;
    }
?>

Question 5:

<?php
    $A = 5;
    $B = 10;
    
    fun($A, $B);
    printf("%d, %d", $A, $B);
    
    function fun(int & $A, int & $B)
    {
        $A = 20;
        $B = 30;
    }
?>

All Answers

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

Answer 1:

Output:

Hello India

Explanation:

In the above program, we defined a user-define function. But we did not call function fun() anywhere then it will not print "Hello World", only "Hello India" will print on the webpage.

Answer 2:

Output:

Hello World
Hello India

Explanation:

In the above program, we defined a user define function. Then we called function fun() then it will print "Hello World", and  "Hello India" will print on the webpage.

Answer 3:

Output:

5, 10

Explanation:

In the above program, we defined a user define function fun() that contains two parameters $A and $B. We modified the values of $A and $B, but it will not reflect at the calling position. Because here we used the call by value mechanism for parameter passing. Then "5,10" will be printed on the webpage.

Answer 4:

Output:

PHP Parse error:  syntax error, unexpected '&' in 
/home/main.php on line 5

Explanation:

The above program will generate syntax error because we cannot use ampersand '&' operator in the function call. To implement call by reference mechanism for parameter passing. We need to use ampersand '&' operator in the function definition.

Answer 5:

Output:

20, 30

Explanation:

In the above program, we defined a user define function fun() that contains two parameters $A and $B. We modified the values of $A and $B, but it will also reflect at calling position. Because here we used the call by reference mechanism for parameter passing. Then "20,30" 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 (Arrays) | set 3...