php - 功能工作不一致
问题描述
我是 PHP 新手。我有一个可以工作的函数(从函数内打印到屏幕上的值正是我所期望的),但只是有时会返回答案(有时它会返回NULL
)。我相信我已将错误与涉及我使用static
PHP 功能的某些事情隔离开来,但我不确定我到底是如何违反/如何修复它。我试图通过创建一个新的非静态变量来存储结果来解决它,这将我的代码从总是返回NULL
到只有时返回NULL
。错误函数是我编写的一个更大的程序套件的一部分,所以我将包括它和我用来检查它是否工作的测试函数。
probGen.php
<?
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);
}
?>
测试.php
<?
/*
* 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))));
}*/
?>
通过测试,我已经能够确定probGen()
如果递归不止一次发生则失败。
解决方案
正如@aynber 的评论(我刚刚写了他的所有功劳,所以这篇文章会有答案)。
调用递归调用时,您应该添加return
(在最后一行)以便将函数的返回值冒泡为probGen
return probGen();
一般来说,让 PHP 返回NULL
有时可能会用作提示,以防返回非值。
也可以看到同样问题的这个问题。
推荐阅读
- c++ - c++ 用“right”格式化 cout 和 setw() 用于字符串和浮点数
- java - 执行程序以读取属性文件时找不到元素“bean”的声明
- freeradius - freeradius 3.0.13 使用自定义属性回复
- c - 什么是 C 中的模块?
- c# - 如何通过linq对列表进行分组并在数据表中显示内部列表
- vba - 循环遍历充满 excel 文件的文件夹中的特定工作表并提取为 .csv
- python - 记录的重复数据删除
- python-3.x - Networkx 错误:“在 _not_implemented_for 术语中 = {'directed': graph.is_directed()}”
- php - Google 云打印无法转换为 PDF
- wordpress - 如何在转发器字段中动态填充复选框值