Many functions build-in from php that we often use, such as
print()
, print_r()
, unset()
, etc. Apart from these functions, we can also make our own functions as needed.
Function is a set of instructions wrapped in a block. The function can be reused without having to rewrite the instructions in it.
Functions in PHP can be made with keywords
function
, then followed by the name of the function.
Example:
function namaFungsi(){
//...
}
The instruction code can be written in curly braces (
{...}
).
Example:
function perkenalan(){
echo "Assalamulaikmu, ";
echo "Perkenalkan, nama saya Ardianta<br/>";
echo "Senang berkenalan dengan anda<br/>";
}
Functions that have been created will not produce anything if not called. We can call a function by writing its name.
Example:
perknalan();
So, the complete code is like this:
<?php
// mmbuat fungsi
function perkenalan(){
echo "Assalamulaikmu, ";
echo "Perkenalkan, nama saya Ardianta<br/>";
echo "Senang berkenalan dengan anda<br/>";
}
// memanggil fungsi yang sudah dibuat
perkenalan();
echo "<hr>";
// memanggilnya lagi
perkenalan();
?>
the result:
Functions with Parameters
In order for instructions in more dynamic functions, we can use parameters to enter a value into a function. This value will be processed in the function.
For example, in the example of the previous function, it is not possible for the name printed to be an ardianta only and the greeting used is not always an assalamualaikum . So, we can add parameters like this:
<?php
// mmbuat fungsi
function perkenalan($nama, $salam){
echo $salam.", ";
echo "Perkenalkan, nama saya ".$nama."<br/>";
echo "Senang berkenalan dengan anda<br/>";
}
// memanggil fungsi yang sudah dibuat
perkenalan("Muhardian", "Hi");
echo "<hr>";
$saya = "Indry";
$ucapanSalam = "Selamat pagi";
// memanggilnya lagi
perkenalan($saya, $ucapanSalam);
?>
The result:
Parameters with Default Values
We can give a default value in the parameter. The default value functions to fill in the value of a parameter, if the parameter is not filled in the value.
For example: I forgot to fill in the greeting parameter , the program will error. Therefore, we need to provide a default value so that there is no error .
Example:
<?php
// mmbuat fungsi
function perkenalan($nama, $salam="Assalamualaikum"){
echo $salam.", ";
echo "Perkenalkan, nama saya ".$nama."<br/>";
echo "Senang berkenalan dengan anda<br/>";
}
// memanggil fungsi yang sudah dibuat
perkenalan("Muhardian", "Hi");
echo "<hr>";
$saya = "Indry";
$ucapanSalam = "Selamat pagi";
// memanggilnya lagi tanpa mengisi parameter salam
perkenalan($saya);
?>
The result:
A function that returns a value
The results of processing the value of a function we may need for subsequent processing. Therefore, we must create a function that can return a value.
Returning values in functions can use keywords
return
.
Example:
<?php
// membuat fungsi
function hitungUmur($thn_lahir, $thn_sekarang){
$umur = $thn_sekarang - $thn_lahir;
return $umur;
}
echo "Umur saya adalah ". hitungUmur(1994, 2015) ." tahun";
?>
The result:
Call Function in Function
The functions that we have created, can also be called in other functions.
Example:
<?php
// membuat fungsi
function hitungUmur($thn_lahir, $thn_sekarang){
$umur = $thn_sekarang - $thn_lahir;
return $umur;
}
function perkenalan($nama, $salam="Assalamualaikum"){
echo $salam.", ";
echo "Perkenalkan, nama saya ".$nama."<br/>";
// memanggil fungsi lain
echo "Saya berusia ". hitungUmur(1994, 2015) ." tahun<br/>";
echo "Senang berkenalan dengan anda<br/>";
}
// memanggil fungsi perkenalan
perkenalan("Ardianta");
?>
The result:
Recursive function
The recursive function is a function that calls itself. This function is usually used to solve factorial problems, fibbonaci numbers, dynamic programming, etc.
Examples of recursive functions:
<?php
function faktorial($angka) {
if ($angka < 2) {
return 1;
} else {
// memanggil dirinya sendiri
return ($angka * faktorial($angka-1));
}
}
// memanggil fungsi
echo "faktorial 5 adalah " . faktorial(5);
?>
The result:
0 Komentar untuk "Learning PHP: Understanding Procedures and Functions"
Silahkan berkomentar sesuai artikel