Oct
24
Posted on 24-10-2007
Filed Under (Function) by chintan

PHP Functions

In this lesson we will show you how to create your own functions.For a situation and examples of the built-in functions, please visit our PHP Reference.

Create a PHP Function

A function is a block of code that can be executed whenever we need it.

(1) All functions start with the word “function()”

(2) Name the function – It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)

(3) Add a “{” – The function code starts after the opening curly brace

(4) Insert the function code

(5) Add a “}” – The function is finished by a closing curly brace

Example

A simple function that writes my name when it is called:

<html><body>

<?php

function writeMyName()

{

echo “chintan”;

}
writeMyName();

?>

</body></html>

Use a PHP Function

Now we will use the function in a PHP script:

<html><body>

<?php

function writeMyName()

{

echo “chintan”;

}

echo “Hello world!<br />”;

echo “My name is “;

writeMyName();

echo “.<br />That’s right, “;

writeMyName();

echo ” is my name.”;

?>

</body>

</html>

The output of the code above will be:

Hello world!

My name is chintan.

That’s right, chintan is my name.

PHP Functions – Adding parameters

Our first function (writeMyName()) is a very simple function. It only writes a static string.

To add more functionality to a function, we can add parameters. A parameter is just like a variable.

You may have noticed the parentheses after the function name, like: writeMyName(). The parameters are specified inside the parentheses.

Example 1

The following example will write different first names, but the same last name:

<html><body>

<?php

function writeMyName($fname)

{

echo $fname . ” Refsnes.<br />”;

}

echo “My name is “;

writeMyName(“chintan”);

echo “My name is “;

writeMyName(“Den”);

echo “My name is “;

writeMyName(“James”);

?>

</body></html>

The output of the code above will be:

My name is chintan Refsnes.

My name is Den Refsnes.

My name is James Refsnes.

Example 2

The following function has two parameters:

<html><body>

<?php

function writeMyName($fname,$punctuation)

{

echo $fname . ” Refsnes” . $punctuation . “<br />”;

}

echo “My name is “;

writeMyName(“chintan”,”.”);

echo “My name is “;

writeMyName(“Den”,”!”);

echo “My name is “;

writeMyName(“James”,”…”);

?>

</body>

</html>

The output of the code above will be:

My name is chintan Refsnes.

My name is Den Refsnes!

My name is James Refsnes…

PHP Functions – Return values

Functions can also be used to return values.

Example

<html><body>

<?php

function add($x,$y)

{

$total = $x + $y;

return $total;

}

echo “1 + 10 = ” . add(1,10);

?>

</body></html>

The output of the code above will be:

1 + 10= 11

Share These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Live
  • StumbleUpon
  • Technorati
  • YahooMyWeb
(0) Comments    Read More   
Post a Comment

You must be logged in to post a comment.