Fungsi merupakan sebuah blok program
yang dapat dieksekusi kapanpun kita memerlukannya.
Langsung aja biar kita lebih memahaminya simak contohnya dibawah ini:
Contoh 1
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>
|
Contoh 2
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>
|
Ouput program:
Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.
|
Fungsi
berparameter
Contoh 3
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>
|
output program:
My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.
|
Contoh 4
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
</body>
</html>
|
output:
My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...
|
Fungsi yang
mengembalikan sebuah nilai
Contoh 5
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>
|
output :
1 + 16 = 17
|
oke sampai disini dulu, semoga ilmunya bermanfaat bagi semuanya.. silahkan di share ya biar ilmunya bermanfaat bagi semua yang membutuhkan dan tentunya ilmu bermanfaat akan menolong kita diakhirat nanti.. he
No comments:
Post a Comment