Dedicated Server Forum

The Hivelocity Dedicated Server Discussion Forum is a place for Hivelocity clients to meet and chat about all things server. From server promotions to server issues, our technical staff, sales, billing, management and Upper management will be actively involved in these discussions. No question is too big or small, no topic is too stupid to be discussed here. We want all of our clients to feel like this is their home away from home on the net.


 
 
In order to get support you will need to enter your registration details:
 
Please enter your search criteria:
 
 

Dedicated Server Forums by Hivelocity Award winning Dedicated Server hosting > Miscellaneous > Coding Samples » PHP - Functions


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2009, 09:48 AM
Administrator
 
Join Date: Jul 2009
Posts: 578
Default PHP - Functions

What is a function ?

A function is a portion of code which performs a specific task and it independent from the rest of the code.

PHP Code:
function add($numberOne$numberTwo) {
   
$sum $numberOne $numberTwo;
   return 
$sum;

This function takes two parameters, $numberOne and $numberTwo. Then assigns $sum to the sum of the two variables. After that we return the variable $sum.

Lets explain each part !

PHP Code:
function add($numberOne$numberTwo
We start a function by using the keyword function. Every function has a name (unless it's a closure, anonymous function, etc, but that's another subject). So the name of our function is add. In our function add we are passing two parameters $numberOne and $numberTwo. This allows us to pass values to our function, you are able to have functions that get passed no values, it would look like this
PHP Code:
 function add() { } 
Next we add the two parameters in and assign it to the variable $sum. This is pretty self explanatory.

PHP Code:
 return $sum
This returns the value we assigned to $sum. In the case of this function we could take this value and print it to the screen.

PHP Code:

$add 
add(1,1); // Return value is 2
echo $add// Print 2 to the screen 
Well that's it for this tutorial ! Hope it helps you out !

Last edited by HV-Adam; 08-06-2009 at 09:51 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:54 AM.