Home >Backend Development >PHP Tutorial >Make a PHP custom function return multiple values_PHP Tutorial
PHP custom functions only allow the return statement to return a value. When the return is executed, the entire function will terminate. Sometimes when we require a function to return multiple values, using return cannot output the values one after another. But one thing that cannot be ignored is that the return statement can return any type of variable. This is the key to making the custom function return multiple values. Please look at the code:
function results($string)
{
$result = array();
$result[] = $string;// Original string
$result[] = strtoupper($string);//Change all to uppercase
$result[] = strtolower($string);//Change all to lowercase
$result[] = ucwords($string);//Change the first letter of the word to uppercase
return $result;
}
$multi_result = results('The quick brown fox jump over the lazy dog') ;
print_r($multi_result);
?>
Output result:
Array
(
[0] => The quick brown fox jump over the lazy dog
[1] => THE QUICK BROWN FOX JUMP OVER THE LAZY DOG
[2] => the quick brown fox jump over the lazy dog
[3] => The Quick Brown Fox Jump Over The Lazy Dog
)
The above code creates a $result array, then adds the value that has been processed and is waiting for output to $result as an element, and finally outputs $result, like this This achieves the purpose of the custom function returning multiple values.