Fungsi/Function php


Fungsi merupakan sebuah blok program yang dapat dieksekusi kapanpun kita memerlukannya.

Program10-1.php
<html>
<body>
<?php
function writeMyName()
  {
  echo "Kai Jim Refsnes";
  }
writeMyName();
?>
</body>
</html>

Program10-2.php:
<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


Program10-3.php
<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.

 

Program10-4.php
<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


Program10-5.php
<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 komentar:

Post a Comment

Copyright © 2014 Dunia Naeta All Right Reserved