search
Homephp教程php手册PHP自定义函数返回多个值

PHP自定义函数返回多个值

Jun 13, 2016 am 09:46 AM
phpreturnvaluefunctionMultipleimplementusecustomizestatementreturn

PHP自定义函数只允许用return语句返回一个值,当return执行以后,整个函数的运行就会终止。

有时要求函数返回多个值时,用return是不可以把值一个接一个地输出的。

不可忽视的一点是,return语句可以返回任何类型的变量,这就是使自定义函数返回多个值的关键。

 

请看代码:

  1. //自定义函数返回多值  
  2. //by www.jbxue.com  
  3. function results($string)  
  4. {  
  5.     $result = array();  
  6.     $result[] = $string;//原字符串  
  7.     $result[] = strtoupper($string);//全部换成大写  
  8.     $result[] = strtolower($string);//全部换成小写  
  9.     $result[] = ucwords($string);//单词的首字母换成大写  
  10.   
  11.   
  12.     return $result;  
  13. }  
  14. $multi_result = results('The quick brown fox jump over the lazy dog');  
  15. print_r($multi_result);  
  16. ?>  
输出结果:
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
)
以上的代码创建了一个$result数组,然后把处理完毕并等待输出的值添加到$result中作为一个元素,最后把$result输出,这样做就实现了自定义函数返回多个值的目的。 
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.