PHP Custom functions return multiple values _php tutorial

Source: Internet
Author: User
Tags vars
The PHP Custom function only allows you to return a value with a return statement, and when the return is executed, the entire function is terminated.

Sometimes when a function is required to return multiple values, it is not possible to output the value one by one, using return.

One thing to ignore is that the return statement can return any type of variable, which is the key to making the custom function return multiple values.

Please look at the code:

  1. //Custom function returns multi-value
  2. //by www.jbxue.com
  3. function results ($string)
  4. {
  5. $result = Array ();
  6. $result [] = $string ; //Original string
  7. $result [] = Strtoupper ($string); //Replace All caps
  8. $result [] = Strtolower ($string); //change all to lowercase
  9. $result [] = Ucwords ($string); //The first letter of the word is changed to uppercase
  10. return $result;
  11. }
  12. $multi _result = results (' The quick brown fox jump over the lazy dog ');
  13. Print_r ($multi _result);
  14. ?>
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 processed and waiting output values to $result as an element, and finally outputs the $result, which enables the custom function to return multiple values.

http://www.bkjia.com/PHPjc/654281.html www.bkjia.com true http://www.bkjia.com/PHPjc/654281.html techarticle The PHP Custom function only allows you to return a value with a return statement, and when the return is executed, the entire function is terminated. Sometimes when a function is required to return multiple values, return is not possible .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.