ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP º¯Êý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP º¯Êý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-06-17 ä¯ÀÀ:8136 :: ::

The real power of PHP comes from its functions.
PHPµÄÇ¿´ó¹¦ÄÜÖ÷ÒªÌåÏÖÔÚº¯ÊýÀï¡£

In PHP - there are more than 700 built-in functions available.
PHPÖй²°üº¬700¶à¸ö¿ÉÓõÄÄÚÖú¯Êý¡£


PHP Functions
PHPº¯Êý

In this tutorial we will show you how to create your own functions.
ÔÚÕâ¸ö½Ì³ÌÖÐÎÒÃǽ«¸æËßÄãÈçºÎ´´½¨×Ô¼ºµÄº¯Êý

For a reference and examples of the built-in functions, please visit our PHP Reference.
Èç¹ûÄãÏëÁ˽â¸ü¶à¹ØÓÚPHP²ÎÊýºÍÄÚÖú¯ÊýµÄÀý×Ó£¬Çë·ÃÎÊPHP²ÎÊý¡£


Create a PHP Function
´´½¨Ò»¸öPHPº¯Êý

A function is a block of code that can be executed whenever we need it.
º¯Êýʵ¼ÊÉÏÊÇÒ»¸öͳһµÄ´úÂë¿é£¬Äã¿ÉÒÔËæÊ±µ÷ÓÃËü¡£

Creating PHP functions:
´´½¨PHPº¯ÊýµÄ·½·¨£º

  • All functions start with the word "function()"
    ËùÓк¯ÊýµÄ¿ªÍ·±ØÐë¼ÓÉÏ“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:
ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄPHPº¯Êý°¸Àý£¬µ±ÎÒÃǵ÷ÓÃËüʱ£¬Ëü¿ÉÒÔÊä³öÎÒÃǵÄÃû×Ö£º

<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>

 


Use a PHP Function
ʹÓÃÒ»¸öPHPº¯Êý

Now we will use the function in a PHP script:
ÏÂÃæÎÒÃǽ«Ê¹ÓÃPHP½Å±¾³ÌÐòÊéдº¯Êý£º

<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
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 Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.

 


PHP Functions - Adding parameters
PHPº¯Êý——Ìí¼Ó²ÎÊý

Our first function (writeMyName()) is a very simple function. It only writes a static string.
ÎÒÃǵĵÚÒ»¸öº¯Êý(writeOurName())ÊÇÒ»¸ö·Ç³£¼òµ¥µÄº¯Êý£¬ËüÖ»ÊÇÊéдÁËÒ»¶Î¾²Ì¬µÄ×Ö·û´®¡£

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
°¸Àý1

The following example will write different first names, but the same last name:
ÏÂÃæµÄÀý×ÓÖУ¬Êä³öÃû×ֵIJ»Í¬£¬ÐÕÏàͬ£º

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

The output of the code above will be:
ÉÏÊö´úÂëÊä³öµÄ½á¹û£º

My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.

Example 2
°¸Àý2

The following function has two parameters:
ÏÂÃæµÄº¯ÊýÖаüº¬Á½¸ö²ÎÊý£º

<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
</body>

</html>

The output of the code above will be:
ÉÏÊö´úÂëÊä³öµÄ½á¹û£º

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

 


PHP Functions - Return values
PHPº¯Êý——·µ»ØÖµ

Functions can also be used to return values.
º¯ÊýÒ²¿ÉÒÔÓÃ×÷·µ»Ø“Öµ[value]”£º

Example
°¸Àý

<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>

The output of the code above will be:
ÉÏÊö´úÂ뽫Êä³öÏÂÃæµÄ½á¹û£º

1 + 16 = 17

ÆÀÂÛ (3) 1 All