How to make function in PHP
Function or a number of statements that are packed in a name. This name can then be called multiple times in several places in the program.
The purpose of the use of the function is:
The purpose of the use of the function is:
- Facilitate in developing programs
- Reduce the size of the program
To create a function, must follow the syntax as follows:
function function_name($parameter1, $parameter2)
{
statement1;
statement2;
}
Example 1: create a function that has no parameters
<?
function OpenTable()
{
echo "<table align=center width=\"80%\" border=0 cellspacing=1
cellpadding=0 bgcolor=#555555><tr><td>\n";
echo "<table width=\"100%\" border=0 cellspacing=1 cellpadding=8
bgcolor=#ffffff><tr><td>\n";
echo "<center>";
}
function CloseTable()
{
echo "</td></tr></table></td></tr></table>\n";
}
?>
<html>
<head>
<title> Function 1 </title>
</head>
<body>
<?php
OpenTable();
print ("this is the first table");
CloseTable();
print ("<br>");
OpenTable();
print ("this is the second table");
CloseTable();
?>
</body>
</html>
Example 2: create a function that has parameters
<?
function OpenTable($color1, $color2)
{
echo "<table align=center width=\"80%\" border=0 cellspacing=1
cellpadding=0 bgcolor=\"$color1\"><tr><td>\n";
echo "<table width=\"100%\" border=0 cellspacing=1 cellpadding=8
bgcolor=\"$color2\"><tr><td>\n";
echo "<center>";
}
function CloseTable()
{
echo "</td></tr></table></td></tr></table>\n";
}
?>
<html>
<head>
<title> Function 2 </title>
</head>
<body>
<?php
OpenTable("red", "#dddddd");
print ("This is the first table");
print ("<table border=1 width=100%>");
print ("<tr><td width=33% align=center> Column 1 </td>");
print ("<td width=33% align=center> Column 2 </td>");
print ("<td width=* align=center> Column 3 </td> </tr>");
print ("</table>");
CloseTable();
print ("<br>");
OpenTable("blue", "white");
print ("This is the second table");
CloseTable();
?>
</body>
</html>
0 comments:
Post a Comment