English 中文(简体)
我如何在职能(最佳做法)中回归多种变量?
原标题:How should I return multiple variables in a function (for best practices)?

了解最佳做法对于类似情况有何好处:

一种功能,即回归多种变量——应当如何归还这些变量?

like this (globalizing):

function myfun(){

global $var1,$var2,$var3;

$var1="foo";
$var2="foo";
$var3="foo";

}//end of function

或如此(恢复阵列):

function myfun(){

$var1="foo";
$var2="foo";
$var3="foo";

$ret_var=array("var1"=>$var1,"var2"=>$var2,"var3"=>$var3);

return $ret_var;

}//end of function

我进行了业绩测试,它期望使用阵列的速度更快(在几次复习之后):

array took: 5.9999999999505E-6
global took: 2.0999999999938E-5

但是,我很想知道,对于像这样简单的情况,什么方法是最佳做法?

最佳回答

你们绝对应该回到一个阵列。 这一点非常清楚。 您甚至可以使用list 用于模拟多个回报值a-la python:

function foon() {
   return array( one ,  two ,  three );
}
list($one, $two, $three) = foon();

Edit:在某些情况下,一个联系阵列也非常合适。 我无法想象,业绩差别将会导致这种清晰度的丧失。

问题回答

http://de.php.net/manual/en/language. References.php“rel=“nofollow” http://de.php.net/manual/en/English.参比。

例:

function myfun(&$var1, &$var2, &$var3)
{
    $var1="foo";
    $var2="foo";
    $var3="foo";
}

或者返回一个阵列。

AND NEVER USE GLOBALS! It is very bad design and not maintainable.

联系阵列方案从维护守则的角度来看更为可取,因为你不再占用全球名称空间。 其他国家也更容易阅读你的法典。

您可以返回一份Array和使用清单,以获取这些价值,见http://www. . . . net/manual/en/tus.list.htm“rel=“noestlow”http://www. . . . . net/manual/en/stalist. 。

function multiple() {
    return array( val1 ,  anotherval );
}

list($var1, $var2) = multiple();

echo "$var1, $var2";

对我来说,我不知道这种速度是多么快,但也许在你的法典中看得更清洁。

由于这在谷歌中排名如此之高,而且在找到对如此简单的东西的明确答案时,我会这样做。

1. 提供完整的答案,并彻底澄清如何将你实际上独立于你的职能之外的多种变数退回:

  function  myFunction() {

      //your code that returns your results to variables below

     $var1 =  message 1 ;
     $var2 =  message 2 ;
     $var3 =  message 3 ;

     // must put all variables inside an array to get them outside of function
     $return_array = ($var1, $var2, $var3);

     // return your array inside function
     return $return_array;
  }



 // call function (returns all variables in array)
 $get_results = myFunction();

 // call and assign individual variables outside of function
 $var1 = $get_results[0];
 $var2 = $get_results[1];
 $var3 = $get_results[2];



 // now you can use individual variables outside of function for example:

 echo $var2;   //will now output: message 2
 echo $var3;   //will now output: message 3

如果你利用数据库查询结果,你将比可能更需要将链接变数输入这一功能。 例如,我们将说,db美元是我们的数据库连接,你只是修改以下两条法典:

  //original code to change
  function  myFunction() {
  //change to
  function  myFunction($db) {

  //original code to change
  $get_results = myFunction();
  //change to
  $get_results = myFunction($db);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签