Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code shows a blank screen

PHP
<?php

function add($first, $second)
{

global $result ;
$result = $first + $second ;

}


echo add(4,5) ;


?>



but this code shows the output as 9

PHP
<?php

function add($first, $second)
{

global $result ;
$result = $first + $second ;

}


add(4,5) ;
echo $result ;


?>



What is the reason for this difference?
Posted
Comments
Maarten Kools 1-Nov-13 7:28am    
add() does not return a result, so echo add() won't output anything.
CPallini 4-Nov-13 16:06pm    
My (virtual) 5.
IAM_Dhruv 1-Nov-13 8:13am    
ok. thanks. Had this confusion. Its clear now.

1 solution

What is your function sending back with it's return statement?

Nothing - since you don't have one.

Add something like
return $result;
to the end and echo'ing the function will echo the return value;



 
Share this answer
 
Comments
CPallini 4-Nov-13 16:06pm    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900