Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


July 8, 2006

Php Functions

Filed under: Intro PHP Tutorials — phpdeveloper @ 7:36 am

Functions in PHP

Functions are the robots/machines of any programming languages and in PHP they just the same. They are mainly used to make code reusable and easy to read. If you need to record a show on television only one time, do you program the VCR? However, if you need to record the same show everday, you use the timer and the programmer on the VCR to make it happen.

Use of Functions
You have been using functions all along in your PHP programming without actually calling them functions. So instead of having to enter the code over and over again, you just made a simple function call to the already defined function .

The most important question to ask yourself when you are thinking about writing a function, is how many times am i gonna be using this same or reasonable same programming code to accomplish a task? If you see yourself having to use the code over and over again, then you need to put it in a function.

Example of a PreDefined Function
print( “I am a predefined function. yes i meaning PRINT”);

See now, I told you that you used functions all along. Behind the scenes there is code that tells print what to do with the information is it sent i.e, What information you want to have printed.

Syntax of Functions
A function, in simple terms takes input(argument(s)) and produces output(results(s)). The code within a function does operations on the inputs and then produces output.

Function Definition

function my_function ($agruement1, $agruement2, $agruement3)
{
return $fctval;
}

The above function, takes three parameters, and then returns a value. These parameters are passed by value,. Remember that for the future

function my_other_function ()
{
/* any valid php code. */
}

The above function, just does operations with no parameters, this can be used in many ways including writing output to the screen.

Function Calls

$returnedvalue = my_function($value1, $value2, $value3);

Since my_function returns a value, we can use it in any arithmetic function, just like we would use any other variable.

my_other_function();

Now that is just a basic introductions to functions, we will discuss functions in more details in a future tutorial.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.