How do I return a value from a function?
The first time you create a function you might do something like this and get confused:
<?php
$value = 20;
function timestwo($value) { $result = $value * 2; }
$doubled = timestwo($value);
echo "$value doubled is $doubled";
//prints: 20 doubled is
?>
... and you get no value! That's because you need to use 'return' in the function, and it then works:
<?php
$value = 20;
function timestwo($value) { $result = $value * 2; return $result; }
$doubled = timestwo($value);
echo "$value doubled is $doubled";
//20 doubled is 40
?>
Comment on this Question and Answer >>>
ASK A QUESTION
More functions PHP Questions
How can I call online a PHP function. It works local but when I put it online it does nothing. I call online a php function to load pictures into my flex application. Thanks BartPHP Fatal error does not result in HTTP error code 500. When PHP code throws a Fatal error, I would expect Apache to respond with HTTP 500, not a 200?
WHAT IS THE CORRECT WAY TO OPEN THE FILE "time.text" AS READABLE?
How do I make a variable from outside a function work in that function?
Hey, I need a php script to copy a complete directory to a new one (which is a variable). i.e www.webpage.com/example/ --> www.webpage.com/folder_just_created/. I've looked for hours Kurt
