Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > PHP Forum & Tutorial


PHP Functions


Reply
Views: 3382  
Thread Tools Rate Thread
  #1  
Old 05-04-2009, 04:55 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
Default PHP Functions

The real power of PHP comes from its functions.
In PHP - there are more than 700 built-in functions available.



PHP Functions

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.



Create a PHP Function

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

Creating PHP functions:
  • 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


Reply With Quote
  #2  
Old 05-04-2009, 04:58 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
Example

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

function writeMyName()
{
echo "Kai Jim Refsnes";
} writeMyName();
?>
Reply With Quote
  #3  
Old 05-04-2009, 04:58 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
Use a PHP Function

Now we will use the function in a PHP script:

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

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.
Reply With Quote
  #4  
Old 05-04-2009, 04:59 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
Example 1

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

function writeMyName($fname)
{
echo $fname . " Refsnes.
";
} echo "My name is ";
writeMyName("Kai Jim"); echo "My name is ";
writeMyName("Hege"); echo "My name is ";
writeMyName("Stale");
?>
The output of the code above will be:
My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.
Reply With Quote
  #5  
Old 05-04-2009, 04:59 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
Example 2

The following function has two parameters:





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




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...
Reply With Quote
Reply

New topics in PHP Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)