phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Reuse Code: Using Function in PHP (Part 2)

Tag: Reuse Code   Category:
post: 12 Nov 2007 read: 705


Functions exist in most programming languages. They are used to separate code that performs a single, well defined task. This make the code easier to read and allows us to reuse the code each time we need to do the same task. (Luke Welling and Laura Thomson).

PHP have built in functions which you can use for interacting with files, database, etc. But, when we build application, we need special function that not exist in built in function. Your code will probably be a mixture of existing functins combined with your own logic to perform a task for you.

Basic Structure

A function declaration creates or declares a new function. The declarationbegins with the keyword function, provides the function name, the parameters required, and contains the code that will be executed each time this function called:


function my_function()
{
  echo "Content of my function that will be executed.";
}

We can call our new function with the following statement:


my_function();

A few restrictions naming of your function:

  • Your function cannot have the same name as an existing function
  • Your function name can only contain letters, digits, and underscores.
  • your function name cannot begin with a digit.
Parameters In order to do their work, most functions require one or more parameters. a parameter allows you to pass data into a function.

function Array_to_String( $data )
{
	$string = implode(",",$data);
	echo $string;
}

We call Array_to_String() like this:


$arr = array('One','Two','Three');
Array_to_String( $arr );


Series this article:
Reuse Code: Introduction ( Part 1 )
Reuse Code: Using Function in PHP (Part 2)

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting