• Fungsi ada 2:
• Built-in function
• User defined function
Built-in Function
<?php
function isLeapYear( $year )
{
if (((($year % 4) == 0) &&
(($year % 100) != 0)) ||
(($year % 400) == 0))
$result = TRUE;
else
$result = FALSE;
return( $result );
}
$yr = 2000;
printf( "Year %d ", $yr );
$result = myLog( 8, 2 );
function()
Name: $x $y Name:
Address: 1005 value 1010 Address:
Data: 123 copied 123 Data:
12
Contoh Pass by Value
<?php
function passByValue( $y )
{
$y = 321; // function value
printf( "%s Inside: %d<br />",
str_repeat( " ", 3 ),
$y );
}
$x = 123;
printf( "Before: %d<br />", $x );
passByRef( $x );
printf( "After: %d<br />", $x );
?>
Kelebihan - Kekurangan
• Kelebihan:
• Lebih cepat karena nilai tidak perlu dicopy
• Ideal untuk dataset besar
• Dapat mengembalikan banyak nilai
• Kekurangan: fungsi menjadi kurang ter-
enkapsulasi.
Latihan
echo add( 2, 3 );
?>
Pernyataan include_once
include_once.php db.php
<?php <?php
include_once(‘math.php’); include('misc.php');
?>
include_once(‘html.php’);
include_once(‘db.php’);
?>
Pernyataan require