Okay, in learning php this time I will try to explain how to create a function that can returnmore than one value, or the original term, Returning multiple values from function in php.The essence of these functions is the use of arrays and functions list ();. Yes, here I willtry to give an example function that can separate the first name and last name of a person's name. Create a file named rvalue.php or you can give another name that youwant.
The contents of the file with:
<? php
double_value function ($ namalengkap)
{
$ depanbelakang = explode ("", $ namalengkap);
$ nama_depan = $ depanbelakang [0];
$ nama_belakang = $ depanbelakang [1];
return array ($ nama_depan, $ nama_belakang);
}
$ namalengkapnya = "Loka Dwiartara";
list ($ namadepannya, $ namabelakangnya) = double_value ($ namalengkapnya);
echo "Name:". $ namalengkapnya. "<br>";
echo "Name:". $ namadepannya. "<br>";
echo "Last Name:". $ namabelakangnya;
?>
The core of its functions ...
when a variable used as the contents of the parameter
double_value ($ namalengkap)
separate the first and last name by using a space and enter into an array
$ depanbelakang = explode ("", $ namalengkap);
so that later to be able to access the first name of his use of the variable $depanbelakang [0]
and to access his last name to use the variable $ depanbelakang [0]
then do the return value of the form array
return array ($ nama_depan, $ nama_belakang);
with the function list () take two values that shaped array
list ($ namadepannya, $ namabelakangnya) = double_value ($ namalengkapnya);
then show by using echo function
echo "Name:". $ namalengkapnya. "<br>";
echo "Name:". $ namadepannya. "<br>";
echo "Last Name:". $ namabelakangnya;
That is one example of the algorithm makes the function can return two values. Good luck.
Thanks for reading & sharing Agus Nur Ikhsan
0 komentar:
Posting Komentar