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 access information sent through $_GET on a form?How do I get a variable in a function to retain its value between calls?
difference between fetch_array function & fetch_row function
which function can assign the output to a string variable?
