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>";
Well at least this part is wrong
Should be
But the rest of this code is pretty messy, so....
For example, this logic
is quite flawed. Because the
else
block is not reachable. In otherwords nothing satisfies the else condition because most values goes though theelseif
which has1
and0
in it's range, and many things in PHP evaluate out to1{true}
or0{false}
because of PHP's loose typing.UPDATE
You can test this condition for yourself
Outputs
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.Test it online
Additionally:
Global must be declared first.
We can test this, by setting up a function scope.
Now one would expect this to print
50
, however it prints100
. It executes this way:$wai
outside the function$wai
to = 100 (assignment)foo
$wai
= 50;$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 100This is a very close approximation of your code, it's functionally equivalent Check it yourself
Now if you change the order
It outputs
50
test thisLet'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 fromIf both of the other returns are just
return;
or returnnull
. 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.So
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.
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.