Php 函数的工作方式不一致
我是PHP新手。我有一个可以工作的函数(从函数内部打印到屏幕上的值正是我所期望的),但有时只返回答案(有时返回Php 函数的工作方式不一致,php,arrays,random,Php,Arrays,Random,我是PHP新手。我有一个可以工作的函数(从函数内部打印到屏幕上的值正是我所期望的),但有时只返回答案(有时返回NULL)。我相信我已经将这个错误与我使用PHP的静态特性有关,但我不确定我到底有多违规/如何修复它。我试图通过创建一个新的非静态变量来存储结果来解决这个问题,这将我的代码从总是返回NULL改进为有时只返回NULL。erring函数是我编写的一个更大的程序套件的一部分,因此我将包括它和我用来检查它是否工作的测试函数 probGen.php test.php probGen()按
NULL
)。我相信我已经将这个错误与我使用PHP的静态
特性有关,但我不确定我到底有多违规/如何修复它。我试图通过创建一个新的非静态变量来存储结果来解决这个问题,这将我的代码从总是返回NULL
改进为有时只返回NULL
。erring函数是我编写的一个更大的程序套件的一部分,因此我将包括它和我用来检查它是否工作的测试函数
probGen.php
test.php
probGen()
按预期工作的屏幕截图:probGen()
失败的屏幕截图:通过测试,我已经能够确定,如果递归不止一次,probGen()将失败。作为@aynber评论(我刚才写的这一切都归功于他,所以这篇文章将有一个答案) 调用递归调用时,应添加
return
(在最后一行),以便将probGen
函数的返回值作为return probGen()代码>
一般来说,当返回非值时,让PHP返回NULL
有时可能会用作提示
您还可以看到有相同问题的问题。您需要在else块中返回probGen结果。@aynber我不明白这是怎么回事?在我(公认有限)的理解中,else
将控制权转移到probGen()
,因此函数调用后不会执行任何操作?此外,程序的完成不应该发生在if块中吗?我想这可能是因为我在最后一次赋值后没有减少$max
,所以它会创建一个不确定递归/否则会失败。我现在正在使用手机,一旦我回到笔记本电脑,我会尝试修复。与其将其视为相同的功能,不如将其视为不同的功能。调用函数1,它将返回一个值或调用函数2。函数2向函数1返回一个值,但函数1实际上从未对响应执行任何操作,因此它会在乙醚中丢失。执行returnprobgen(…)
将返回递归函数的响应。从一个函数调用另一个函数不会转移控制权。另一种看待它的方式是你打电话给friend#1问一个问题。他们不知道,所以他们打电话给friend#2,然后再给你回电话告诉你答案。@TobiAlafin addingreturn
会将每次递归调用probGen()
的结果返回到之前的结果,最终返回到初始函数调用。@aynber,Lewis非常感谢你。我知道我哪里出错了。请将您的评论格式化为答案,以便我可以接受。
<?
require_once("randX.php");
require_once("../displayArray.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);
function probGen(array $arr, float $control = 0.01)
/*
* Generates a valid, random probability distribution for a given array of elements, that can be used in conjunction with "probSelect()".
* Input:
$arr: An array of elements.
$control: A value that decides how much mass is allowed to be unilaterally dumped onto one element. A high value would permit distributions where most of the mass is concentrated on one element.
If an invalid value is provided, the default is used.
* Output: An associative array where the keys are the elements in the original array, and the values are their probabilities.
*/
{
$control = ($control <= 1 && $control >= 0)?($control):(0.01); #Use the default value if an invalid number is supplied.
static $result = []; #Initialises $result with an empty array on first function call.
static $max = 1; #Initialises $max with 1 on first function call.
foreach ($arr as $value)
{
$x = randX(0, $max); #Random probability value.
$result[$value] = ($result[$value] + $x)??0; #Initialise the array with 0 on first call, and on subsequent calls increment by $x to assign probability mass.
$max -= $x; #Ensures that the probability never sums to more than one.
}
print("<br>sum = ".array_sum($result)."<br><br>");
displayArray($result);
/*
* After the execution of the above code, there would be some leftover probability mass.
* The code below adds it to a random element.
*/
$var = array_values($arr);
if($max <= $control) #To limit concentration of most of the probability mass in one variable.
{
$result[$var[rand(0,(count($var)-1))]] += $max; #Selects a random key and adds $max to it.
displayArray($result);
print("<br>sum = ".array_sum($result)."<br><br>");
$sol = $result;
return $sol;
}
else
probGen($arr, $control);
}
?>
<?
/*
* This file contains some functions that can be used for assigning probabilities to an array of elements or selecting an element from said array with a given probability.
*/
require_once("../displayArray.php");
require_once("confirm.php");
require_once("randX.php");
require_once("probGen.php");
require_once("probSelect.php");
$test = ["California" => 0.37, "Florida" => 0.13, "New York" => 0.28, "Washington" => 0.22];
$testX = array_keys($test);
var_dump(probGen($testX));
/*for ($i=0; $i < 100 ; $i++)
{
print(var_dump(confirm(probGen($testX))));
}*/
?>