PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I return a value from a function?


The clue is in the question - you use the keyword 'return'.

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
?>



ASK A QUESTION

More functions PHP Questions

Is there a way to use readfile() and not have it display the byte number at the bottom?

Can we use include ("abc.php") two times in PHP page "makeit.php" ?

How can I access information sent through $_POST on a form?

How do I get a variable in a function to retain its value between calls?

how can i get the data by using get(),post()methods?



Custom Search