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;
}
?>
Answer 1:
Output:
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:
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:
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:
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:
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