Home arrow PHP arrow Page 4 - PHP Functions

Creating Functions that Return a Value - PHP

If you're looking for a way to save time when you program, look no further. Creating functions lets you reuse code that you've used before without having to rewrite the whole thing. Keep reading to learn how.

  1. PHP Functions
  2. Functions that Take Arguments
  3. Setting Default Values
  4. Creating Functions that Return a Value
  5. Using Variables in Functions
By: Jacques Noah
Rating: starstarstarstarstar / 46
August 01, 2006

print this article



In the last section of this article we discuss functions that return a value. In the previous section we already touched on this kind of function. There are only two differences between the previous functions we discussed and functions that return a value. First, you use the return statement within the function. Second, you assign the result to a variable.

The syntax of a function that returns a value is something like this:

 function function_name($argument1,$argument2){


return $value;


As with our randpass() function in earlier sections, to call a function that returns a value we do this:

$variable = function_name($argument);

Another example is:

function sayhello($argument){

return "Hello $argument!"


A user defined function usually returns just a single value, but it can also return more than one value when arrays are used. Here's a example of how it's done:

function fname($arg1,$arg2,$arg3){

//your statements here

return array($value1,$value2,$value3);


....and here's how you'd use the function.....


The $value1 from the function is assigned to $val1 and $value2 is assigned to $val2 and so forth.

>>> More PHP Articles          >>> More By Jacques Noah

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: