Senin, 30 Juli 2012

How to make the function

Make the Function

 


In this chapter I will show you how to make a php function. To keep the script from being executed when the page loads, you can put it into a function. A function will be executed by a call to the function. You can call a function from anywhere in halaman.Membuat PHP Function
A function will be executed by a call to the PHP Function.
syntax:
function functionName ()
{
code to be executed;
}
PHP Function Manual:
Give the function a name that reflects what its function is. The function name can start with a letter or underscore (not a number).
A simple function that writes my name when the function is called:
<html>
<body>
<? Php
writeName function ()
{
echo "AWP";
}
echo "My name is";
writeName ();
?>
</ Body>
</ Html>
Output:
My name is AWP
Adding a parameter in PHP Function:
To add more functionality to a function, we can add parameters. A parameter is sepertivariabel.
Parameters specified after the function name, in parentheses.
The following example will write different first names, but the same last name:
<html>
<body>
<? Php
writeName function ($ fname)
{
echo $ fname. "Refsnes. <br />";
}
echo "My name is";
writeName ("Kai Jim");
echo "My sister's name is";
writeName ("Hege");
echo "My brother's name is";
writeName ("Stale");
?>
</ Body>
</ Html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
in Example 2
The following function has two parameters:
<html>
<body>
<? Php
writeName function ($ fname, $ punctuation)
{
echo $ fname. "Refsnes". $ Punctuation. "<br />";
}
echo "My name is";
writeName ("Kai Jim", ".");
echo "My sister's name is";
writeName ("Hege", "");
echo "My brother's name is";
writeName ("stale", "?");
?>
</ Body>
</ Html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Stale Refsnes?
PHP Function - Return values
To let the function returns a value, use the return statement.
example
<html>
<body>
<? Php
function add ($ x, $ y)
{
$ Total = $ x + $ y;
return $ total;
}
echo "1 + 16 =". add (1.16);
?>
</ Body>
</ Html>
Output:
1 + 16 = 17

Tidak ada komentar:

Posting Komentar