Php function return value

Source: Internet
Author: User
The return value of the php function. In fact, the php function can return one or more values. you can use the return keyword to return a variable or an array. Return will stop the program at return and return the specified variable.

Php function return value. in fact, the php function can return one or more values. you can use the return keyword to return a variable or an array. return will stop the program at return and return the specified variable. let's take an example today:

The instance code is as follows:

  1. ';
  2. Function she ($ a, $ B, $ c)
  3. {
  4. Return array ($ c, $ B, $ );
  5. }
  6. List ($ x, $ y, $ z) = she (2, 3, 4 );
  7. Echo '$ x ='. $ x. '$ y ='. $ y. '$ z ='. $ z;
  8. ?>
  1. The execution result is as follows:
  2.  
  3. Function add ($ shu)
  4. {
  5. Return $ shu + 1;
  6. }
  7. Echo add (2 ).'
  8. ';
  9. Function she ($ a, $ B, $ c)
  10. {
  11. Return array ($ c, $ B, $ );
  12. }
  13. List ($ x, $ y, $ z) = she (2, 3, 4 );
  14. Echo '$ x ='. $ x .'
  15. $ Y = '. $ y .'
  16. $ Z = '. $ z;
  17. ?>

Php function, how to return multiple return values? (the function cannot return multiple values, but you can return an array to obtain similar results .)

The instance code is as follows:

  1. Function results ($ string)
  2. {
  3. $ Result = array ();
  4. $ Result [] = $ string; // original string
  5. $ Result [] = strtoupper ($ string); // replace all with uppercase letters
  6. $ Result [] = strtolower ($ string); // replace all with lowercase letters
  7. $ Result [] = ucwords ($ string); // replace the first letter of a word with an uppercase letter.
  8. Return $ result;
  9. }
  10. $ Multi_result = results ('The quick brown fox jump over The lazy dog ');
  11. Print_r ($ multi_result );
  12. ?>
  13. Output result:
  14. Array
  15. (
  16. [0] => The quick brown fox jump over the lazy dog
  17. [1] => THE QUICK BROWN FOX JUMP OVER THE LAZY DOG
  18. [2] => the quick brown fox jump over the lazy dog
  19. [3] => The Quick Brown Fox Jump Over The Lazy Dog
  20. )

Reference: This function returns three values. one is the function return and the other two are referenced.

The instance code is as follows:

  1. Test (& $ a, & $ B ){
  2. $ A = 1000;
  3. $ B = 12000;
  4. Return $ a + $ B;
  5. }
  6. $ A = 10;
  7. $ B = 12;
  8. $ C = test ($ a, $ B); // note that no & is found here.
  9. // Display the modified value
  10. Echo $;
  11. Echo $ B;
  12. Echo $ c; // This is the return value of the function;

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.