PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>PHP FUNCTION> The real power of PHP comes from its functions

The real power of PHP comes from its functions

FROM: AUTHOR: TIME:2008-08-18 HITS:

PHP Functions

In this tutorial we will show you how to create your own functions. phpsu.com is a free phpscool

For a reference and examples of the built-in functions, please visit our PHP Reference. phpsu is a phpschool


Create a PHP Function

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

Creating PHP functions: welcome to phpsu.com

  • All functions start with the word "function()"
  • 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)
  • Add a "{"  - The function code starts after the opening curly brace
  • Insert the function code
  • Add a "}"  - The function is finished by a closing curly brace

Example

A simple function that writes my name when it is called: phpsu.com is a free phpscool

<html>
<body> phpsu提供的php教程
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
} www.phpsu.com
writeMyName();
?>

www.phpsu.com

</body>
</html>
phpsu


Use a PHP Function

Now we will use the function in a PHP script:

http://www.phpsu.com

<html>
<body>
phpsu.com is a free phpscool
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
phpsu is a phpschool
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>

phpsu

</body>
</html>

do you kown phpsu.com?

The output of the code above will be:

phpsu提供的php教程

Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name. www.phpsu.com


PHP Functions - Adding parameters

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

phpsu.com is a free phpscool

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

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

Example 1

The following example will write different first names, but the same last name: phpsu is a phpschool

<html>
<body> phpsu.com
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
phpsu is a phpschool
echo "My name is ";
writeMyName("Kai Jim");

phpsu.com

echo "My name is ";
writeMyName("Hege"); http://www.phpsu.com
echo "My name is ";
writeMyName("Stale");
?>

welcome to phpsu.com

</body>
</html>
phpsu is a phpschool

The output of the code above will be: welcome to phpsu.com

My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes. www.phpsu.com

Example 2

The following function has two parameters: phpsu.com

<html>
<body>

http://www.phpsu.com

<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
phpsu is a phpschool
echo "My name is ";
writeMyName("Kai Jim","."); http://www.phpsu.com
echo "My name is ";
writeMyName("Hege","!"); phpsu
echo "My name is ";
writeMyName("Ståle","...");
?> welcome to phpsu.com
</body>
</html>

phpsu提供的php教程

The output of the code above will be: phpsu.com

My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...

phpsu提供的php教程

 

PHP Functions - Return values

Functions can also be used to return values. phpsu.com is a free phpscool

Example

<html>
<body>
phpsu.com is a free phpscool
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}

phpsu.com is a free phpscool

echo "1 + 16 = " . add(1,16);
?> phpsu.com
</body>
</html> do you kown phpsu.com?

The output of the code above will be:

phpsu is a phpschool

1 + 16 = 17 
phpsu

TITLE:The real power of PHP comes from its functions
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Mon Aug 18 13:51:34 GMT-8 2008