PHP递归问题不发送回值 [英] Php recursion issue not sending back values

查看:64
本文介绍了PHP递归问题不发送回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我先前发布的用于解决复杂问题的代码。
背后的逻辑是-在递归的每次迭代中,函数都应为已经进行过的$ amount减少$ wa,并计算新的$ wa,以找到新的$ amount,直到系统找不到$$表中的金额将更大,然后产生$ wa,并将其作为最后一次迭代进行。
尝试开发它时,代码没有返回任何内容,没有错误,没有回显。我在做什么错?

Here is the code that I am going to use for solving my complicated issue, that I posted earlier. Logic behind that is - on every iteration of the recursion, function should decrease $wa for $amount, that has been already proceeded, and calculate new $wa, to find new $amount, until system could not find the $amount in table that will be bigger then resulted $wa, and proceed it as last iteration. Code is returning nothing, when trying to devel it, no errors, no echo. What am I doing wrong?

    $wa = 0.001;
    $address = $address;
    function recursion($wa)
    {
      $resulte = db_select ('Credit_user','c')
      ->fields ('c')
      ->range(0,1)
      ->orderby('cr_ts', 'ASC')
    ->condition('c.affected','0','=')
    ->condition('c.amount','0','!=')
     ->execute();
    foreach($resulte as $item) {
       $code = $item->redeem_code;
      global $code;
       $amounte = $item->amount;
      global $amounte; 
      $wai = $wa - $amounte;
    global $wai;
    if ($wai < 0){  
      echo "production code that should operate with different 
variations of $amounte and $redeem on last iteration";
      return;
    }
    elseif ($wai >= 0){  echo "production code 
that should operate with different variations 
of $amounte and $redeem on each iteration";

      return;

    } else {
      function recursion($wa){
    }
    }

    }}
    echo $amounte;
    echo "<br>";


推荐答案

至少这部分是错误的

} else {
   function recursion($wa){
}

应该

} else {
   return recursion($wa);
}

但是此代码的其余部分非常混乱,所以....

But the rest of this code is pretty messy, so....

例如,以下逻辑

if ($wai < 0){ 
   //... 
}elseif ($wai >= 0){
    //...
}else{
   //not reachable
   return recursion($wa);
}

有很多缺陷。因为 else 块不可访问。换句话说,没有什么满足其他条件,因为大多数值通过 1 elseif 进行。 0 的范围内,PHP中的很多东西都评估为 1 {true} 0 {false} ,因为PHP的输入很松散。

is quite flawed. Because the else block is not reachable. In otherwords nothing satisfies the else condition because most values goes though the elseif which has 1 and 0 in it's range, and many things in PHP evaluate out to 1{true} or 0{false} because of PHP's loose typing.

您可以自己测试此条件

$tests = [
    false,
    -10,
    1,
    0,
    [],        ''
];

foreach($tests as $wai){
    if ($wai < 0){ 
       echo "1";
    }elseif ($wai >= 0){
        echo "2";
    }else{
       echo "3";
    }
    
    echo "\n"; 
}

输出

2
1
2
2
2
2

正如您所见,没有$ 3来自 else 块,正如我所说,这是不可能达到的。因此,此函数不进行递归,因此无法从所述不存在的递归中返回任何内容。

As you can see there is no #3 which comes from the else block which as I said is impossible to reach. Thus this function does no recursion, and therefor can return nothing from said non-existant recursion.

对其进行测试在线

另外:

$wai = $wa - $amounte;
global $wai;

必须先声明全局。

global $wai;
$wai = $wa - $amounte;

我们可以通过设置函数作用域来对此进行测试。

We can test this, by setting up a function scope.

function foo(){
  $wai = 50;
  global $wai;
  echo $wai;
}

global $wai;
$wai = 100;    

foo();

现在,人们希望它会打印 50 ,但是会打印 100 。它是这样执行的:

Now one would expect this to print 50, however it prints 100. It executes this way:


  1. PHP解析并解释代码

  2. 我们声明了global $ wai 函数外

  3. 我们将 $ wai 设置为= 100(分配)

  4. 我们调用函数 foo

  5. 我们设置局部变量 $ wai = 50;

  6. 然后我们将 $ wai 声明为全局变量,它继承了全局 $ wai 来自全局范围(函数范围之外),其值为100

  7. 然后我们回显该值。

  1. PHP parses and interprets the code
  2. We declare global$wai outside the function
  3. We set $wai to = 100 (assignment)
  4. We call function foo
  5. We set the local variable $wai = 50;
  6. We then declare $wai as a global, and it inherits the value of the global $wai from the global scope (outside the function scope), which is a value of 100
  7. Then we echo that value.

这是您的代码的非常近似值,在功能上等效亲自检查

This is a very close approximation of your code, it's functionally equivalent Check it yourself

现在,如果您更改订单

function foo(){
  global $wai;
  $wai = 50;
  echo $wai;
}

global $wai;
$wai = 100;    

foo();

它输出 50 测试

让我们看看, echo 需要更改功能才能返回。假设递归有效,但没有成功。我必须指出,在某种情况下,人们必须想到它的其他两个部分之一。如果不这样做,我们将进行无限递归,直到PHP内存不足并崩溃或达到最大执行时间为止。因此,我们同意必须通过这些措施。现在如何从

Let's see, the echo functions need to be changed to return. Assuming the recursion worked, but it doesn't. One has to assume that at some point it goes thought one of the other 2 parts of that if condition that I pointed out. If it didn't do that we would have infinite recursion until PHP runs out of memory and crashes or the max execution time is hit. So we're agreed it has to go though those. Now how can it return anything from

return recursion($wa);

如果其他两个收益都只是收益; 或收益 null 。从本质上讲,如果函数的下一次迭代将null递归返回到从递归调用该函数返回的返回值,则该调用也必须返回null。

If both of the other returns are just return; or return null. By it's very nature if the next iteration of the function returns null back to our return from calling said function recursively, then that call must also return null.

echo "production code that should operate with different variations of $amounte and $redeem on last iteration";
return; 

所以

return "production code that should operate with different variations of $amounte and $redeem on last iteration";

换句话说,它几乎都坏了。请不要采取这种错误的方式,我只是指出我认为不对的地方。每个人都必须从某个地方开始学习编码,我有将近8年的专业网页设计和PHP编程经验。但是,我仍然记得学习的感觉,而且我仍然犯很多错误。

In other words, it's pretty much all broken. Please don't take this the wrong way, I'm just pointing out what I see wrong. Everyone has to start somewhere learning to code, I have almost 8 years professional web design and PHP programing experience. But, I still remember what it was like learning and I still make plenty of mistakes.

我已经指出了可以解决的问题,但是对于诸如if逻辑之类的事情却没有让我弄清楚该逻辑应该是什么的方式。我只能指出它的崩溃之处。

I've pointed out fixes for what I could, but for things like the if logic there is no way for me to figure out what that logic was supposed to be. I can only point out where it falls apart.

这篇关于PHP递归问题不发送回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆