Adding parameters in PHP Function

Adding parameters in PHP Function

Our first function writedate() 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: writedate(). The parameters are specified inside the parentheses.

Example 1

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

<html>
<body>
<?php
function writeMyName($myname)
{
echo “$myname Gunawan.<br />”;
}
echo “My name is “;
writeMyName(“Johnson”);
echo “My name is “;
writeMyName(“Tony”);
echo “My name is “;
writeMyName(“Jony”);
?>
</body>
</html>

And output is

My name is Johnson Gunawan.
My name is Tony Gunawan.
My name is Jony Gunawan.

Example 2

The following function has two parameters:

<html>
<body>
<?php
function writeMyName($myname,$adding)
{
echo “$myname Gunawan $adding <br />”;
}
echo “My name is “;
writeMyName(“Johnson”,”,1st son”);
echo “My name is “;
writeMyName(“Tony”,”,2nd son”);
echo “My name is “;
writeMyName(“Jony”,”,3rd son”);
?>
</body>
</html>

and output is

My name is Johnson Gunawan ,1st son
My name is Tony Gunawan ,2nd son
My name is Jony Gunawan ,3rd son

Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]