2008年8月28日

PHP Function To Return Mutiple Values

clipped from www.x-pose.org

PHP Function To Return Mutiple Values

The trick to return more than one value in a function can be applied to any programming language, but this example is in PHP.  Functions are designed to return one value, but there really is no limit to have many values that can be returned.  So lets take a look at the code:

  1. /* Example code */  
  2.   
  3. list($var1, $var2) = returnYankees();  
  4. print "$var1 $var2";  
  5.   
  6. function returnYankees(){  
  7.  $a = "go";  
  8.  $b = "yankees";  
  9.   return array($a,$b);  
  10. }  

The code is self explanitory and I don't think I need to go into too much depth. I will say that the $a or $b values that are returned can also be arrays as well. Now you can make one complex function rather than multiple functions! 

 blog it

沒有留言: