PHP function Introduction Tutorial is every one to learn PHP must master the basis to know oh, okay, let's talk about PHP function examples plus tutorials.
The real power of PHP comes from its functions.
In PHP-there are more than 7 00 built-in features.
-------------------------------------------------- ------------------------------
PHP functions
In this tutorial, we will show you how to build your own functions.
References and examples of built-in functions, please visit our PHP reference.
Create a PHP function
A function is a block of code that can be executed when we need it.
To create a PHP function:
All features start with "function ()"
The function of the name-it should be able to understand the feature not by its name. The name can begin with letters or underscores (not a number)
Add a "("-Functional code open after curly support
The ability to insert code
Add a ")"-this function is mainly completed by the closing of the curved brace
For example
A simple feature that writes my name when it is called:
<?php
function Writemyname ()
{
echo "Kai Jim refsnes";
}
Writemyname ();
? >
Using PHP functions
Now, we will use the PHP script for the function:
<?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.";
? >
Output is.
Hello world!
My name is Kai Jim refsnes.
That's right, Kai Jim refsnes are my name
PHP functions-Add parameters
Our first function (Writemyname ()) is a very simple feature. It just writes a static string.
To add more function functions, we can add parameters. parameter is like a variable.
You may have noticed the function name after parentheses, such as: Writemyname (). In parentheses specified by the parameter.
Example 1
The following example writes the first name differently, but the same last name:
<?php function Writemyname ($fname) {echo $fname.
"Refsnes.<br/>";
The
echo "My name is";
Writemyname ("Kai Jim");
echo "My name is";
Writemyname ("Hege");
echo "My name is";
Writemyname ("stale");
outputs the results.
The
my name is Kai Jim refsnes.
The My name is Hege refsnes.
The My name is stale refsnes.
See the function return value below.
instance.
<?php function Add ($x, $y) {$total = $x + $y;
return $total;
echo "1 + 16 =".
Add (1,16);
output results.
1 + =