Advertise here.

Friday 24 August 2012

PHP Tutorial Lesson #11 Functions


In this chapter we will show you how to create your own functions.

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 may call a function from anywhere within a page.


Create a PHP Function


A function will be executed by a call to the function.


Syntax



function functionName()
{
    code to be executed;
}


PHP function guidelines:


  • Give the function a name that reflects what the function does
  • The function name can start with a letter or underscore (not a number)



Example


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


<html>
<body>

<?php
function writeName()
{
    echo "Mohammed Kateregga";
}

echo "My name is ";
writeName();
?>

</body>
</html>




Output :

My name is Mohammed Kateregga



Adding parameters


To add more functionality to a function, we can add parameters. A parameter is just like a variable.

Parameters are specified after the function name, inside the parentheses.


Example 1


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


<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Kateregga.<br />";
}

echo "My name is ";
writeName("Mohammed");
echo "My sister's name is ";
writeName("Halma");
echo "My brother's name is ";
writeName("Abdulhameed");
?>

</body>
</html>

Output :

My name is Mohammed Kateregga.
My sister's name is Halma Kateregga.
My brother's name is Abdulhameed Kateregga.



Example 2




The following function has two parameters:

<html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Kateregga" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Mohammed",".");
echo "My sister's name is ";
writeName("Halma","!");
echo "My brother's name is ";
writeName("Abdulhameed","?");
?>
</body>
</html>

Output:

My name is Mohammed Kateregga.
My sister's name is Halma Kateregga!
My brother's name is Abdulhameed Kateregga?

Return values

To let a function return 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








0 comments:

Post a Comment