Pilih Laman

sumber: https://www.duniailkom.com/tutorial-belajar-php-cara-penulisan-dan-pembuatan-fungsi-php/

A function is program code designed to complete a specific task. The purpose of separating a code into functions is for practicality and convenience in making the main program. Because if it is used as a function, then to carry out the same task, we just call the function, without the need to re-create the program code.

To create a function in PHP, here is the basic format for creating a function:

function nama_fungsi ($parameter1, $parameter2)
{
   // kode program fungsi
   return $nilai_akhir
}
The word function is an instruction to PHP that we are going to create a function function_name is the name of the function to be written $parameter1, $parameter2 is an intermediate variable that will store the input required for processing the function (arguments). Depending on your needs, you are free to design how many parameters are needed. return is a special command for the function, where the word return instructs PHP that the processing of the function has been completed. return $final_value means that the function will ‘return’ $final_value as the result of the function.
Also note that this function is inside a program block marked with curly braces on the first and last lines of the function.