Home >Backend Development >PHP Tutorial >Make a PHP custom function return multiple values_PHP Tutorial

Make a PHP custom function return multiple values_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:00:00837browse

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317208.htmlTechArticlePHP custom functions only allow the return statement to return a value. When the return is executed, the entire function will run termination. Sometimes when we require a function to return multiple values, it is inappropriate to use return...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn