Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Creating Your Own Functions

By: FYICenter.com

Part:   1   2  3  4  5 

A collection of 19 tips on creating your own PHP functions. Clear answers are provided with tutorial exercises on defining functions, defining arguments, passing references, returning references, argument default values, etc. Topics included in this collection are:

  1. How To Define a User Function?
  2. How To Invoke a User Function?
  3. How To Return a Value Back to the Function Caller?
  4. How To Pass an Argument to a Function?
  5. How Variables Are Passed Through Arguments?
  6. How To Pass Variables By References?
  7. Can You Define an Argument as a Reference Type?
  8. Can You Pass an Array into a Function?
  9. How Arrays Are Passed Through Arguments?
  10. How To Pass Arrays By References?
  11. Can You Define an Array Argument as a Reference Type?
  12. How To Return an Array from a Function?
  13. What Is the Scope of a Variable Defined in a Function?
  14. What Is the Scope of a Variable Defined outside a Function?
  15. How To Access a Global Variable inside a Function?
  16. How Values Are Returned from Functions?
  17. How To Return a Reference from a Function?
  18. How To Specify Argument Default Values?
  19. How To Define a Function with Any Number of Arguments?

How To Define a User Function?

You can define a user function anywhere in a PHP script using the function statement like this: "function name() {...}". Here is a PHP script example on how to define a user function:

<?php 
function msg() {
  print("Hello world!\n");
}
msg(); 
?>

This script will print:

Hello world!

How To Invoke a User Function?

You can invoke a function by entering the function name followed by a pair of parentheses. If needed, function arguments can be specified as a list of expressions enclosed in parentheses. Here is a PHP script example on how to invoke a user function:

<?php 
function hello($f) {
  print("Hello $f!\n");
}
hello("Bob");
?>

This script will print:

Hello Bob!

How To Return a Value Back to the Function Caller?

You can return a value to the function caller by using the "return $value" statement. Execution control will be transferred to the caller immediately after the return statement. If there are other statements in the function after the return statement, they will not be executed. Here is a PHP script example on how to return values:

<?php 
function getYear() {
  $year = date("Y");
  return $year;
}
print("This year is: ".getYear()."\n");
?>

This script will print:

This year is: 2006

How To Pass an Argument to a Function?

To pass an argument to a function, you need to:

  • Add an argument definition in the function definition.
  • Add a value as an argument when invoking the function.

Here is a PHP script on how to use arguments in a function():

<?php 
function f2c($f) {
  return ($f - 32.0)/1.8;
}
print("Celsius: ".f2c(100.0)."\n");
print("Celsius: ".f2c(-40.0)."\n");
?>

This script will print:

Celsius: 37.777777777778
Celsius: -40

(Continued on next part...)

Part:   1   2  3  4  5 


Selected Developer Jobs:

More...