PHP Function With More Than One Value
This article will try to explain how to create a function that can return more than one value, or Returning multiple values from function in PHP. The essence of these functions is the use of arrays and list (); function. Here I will try to give an example function that can separate the first name and last name of a person's name. Create a file named value.php or you can give another name that you want.
The contents of the file with:
< ?php
function double_value($fullname)
{
$firstlast = explode(” “, $fullname);
$first_name = $ firstlast[0];
$last_name = $ firstlast[1];
return array($first_name, $last_name);
}
$myfullname = “John Lennon”;
list($myfirstname, $mylastname) = double_value($myfullname);
echo “Full Name : “.$myfullname.”<br>”;
echo “First Name : “.$myfirstname.”<br>”;
echo “Last Name : “.$mylastname ;
?>
The core of its functions :
When a variable used as the contents of the parameter :
double_value ($fullname)
Separate the first and last name by using a space and enter into an array :
$firstlast= explode ("", $fullname);
So that later can be able to access the first name we can use variable $firstlast[0]
and to access last name we can use variable $firstlast[1]
Then do the return value of the form array :
and to access last name we can use variable $firstlast[1]
Then do the return value of the form array :
return array ($first_name, $last_name);
With the list() function take two values that shaped an array :
list ($myfirstname, $mylastname) = double_value ($myfullname);
Then show by using echo function :
echo “Full Name : “.$myfullname.”<br>”;
echo “First Name : “.$myfirstname.”<br>”;
echo “Last Name : “.$mylastname ;
That is one example of the algorithm makes the function can return two values.
PHP Function With More Than One Value
0 comments:
Post a Comment