bahattab
28-01-2008, 04:27 PM
Using Functions in PHP
Overview: This article shows you how to use functions, create your custom functions and how to effectively work with function's in PHP.
A function is basically a PHP script designed to accomplish a single task (usually a task that will need to be accomplished multiple times). Furthermore the code contained within functions is ignored until the function is called from another part in the script.
Creating your own function
Functions in PHP begin with the keyword function followed by the function name. This function name must adhere to the same criteria as variables except they do not begin with $ character as other PHP variables do.
function ([$var1 [= constant]],
[$var2 [= constant]], ...) {
}
Creating a simple function
A function that prints the string "Hello, Functions!" 5 times View Sample Output
<?php
function myfunction()
{
for($i = 0; $i != 5; $i++)
echo "Hello, Functions!<P>";
}
echo "This is before the function is called<P>";
myfunction();
echo "This is after the function has been called";
echo "<P>";
?>
Passing parameters to functions
We have seen how to create functions, now I will show you how to pass some parameters to a functions, A function parameter is nothing more than a piece of data that the function requires to execute.
Modifying our above example and add parameters to that function View Sample Output
<?php
function myfunction($num, $msg)
{
for($i = 0; $i != $num; $i++)
echo $msg ."<P>";
}
echo "This is before the function is called<P>";
myfunction(5,"This is a function with parameters");
echo "This is after the function has been called";
echo "<P>";
?>
Functions with default parameters
Can we have a default value as a parameter I mean if I dont specify any value can a function pick up a default value?
Yes we can easily build functions that have default parameters.
Modifying the example and adding default parameters to that function View Sample Output
<?php
function myfunction($num, $msg="Default Message: PHP is cool")
{
for($i = 0; $i != $num; $i++)
echo $msg ."<P>";
}
myfunction(5,"This is a function with your parameters");
echo "Now calling the function without the second parameter <P>";
myfunction(4);
?>
http://www.phpbuddy.com/article.php?id=7
Overview: This article shows you how to use functions, create your custom functions and how to effectively work with function's in PHP.
A function is basically a PHP script designed to accomplish a single task (usually a task that will need to be accomplished multiple times). Furthermore the code contained within functions is ignored until the function is called from another part in the script.
Creating your own function
Functions in PHP begin with the keyword function followed by the function name. This function name must adhere to the same criteria as variables except they do not begin with $ character as other PHP variables do.
function ([$var1 [= constant]],
[$var2 [= constant]], ...) {
}
Creating a simple function
A function that prints the string "Hello, Functions!" 5 times View Sample Output
<?php
function myfunction()
{
for($i = 0; $i != 5; $i++)
echo "Hello, Functions!<P>";
}
echo "This is before the function is called<P>";
myfunction();
echo "This is after the function has been called";
echo "<P>";
?>
Passing parameters to functions
We have seen how to create functions, now I will show you how to pass some parameters to a functions, A function parameter is nothing more than a piece of data that the function requires to execute.
Modifying our above example and add parameters to that function View Sample Output
<?php
function myfunction($num, $msg)
{
for($i = 0; $i != $num; $i++)
echo $msg ."<P>";
}
echo "This is before the function is called<P>";
myfunction(5,"This is a function with parameters");
echo "This is after the function has been called";
echo "<P>";
?>
Functions with default parameters
Can we have a default value as a parameter I mean if I dont specify any value can a function pick up a default value?
Yes we can easily build functions that have default parameters.
Modifying the example and adding default parameters to that function View Sample Output
<?php
function myfunction($num, $msg="Default Message: PHP is cool")
{
for($i = 0; $i != $num; $i++)
echo $msg ."<P>";
}
myfunction(5,"This is a function with your parameters");
echo "Now calling the function without the second parameter <P>";
myfunction(4);
?>
http://www.phpbuddy.com/article.php?id=7