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
} else {
function recursion($wa){
}
Should be
} else {
return recursion($wa);
}
But the rest of this code is pretty messy, so....
For example, this logic
if ($wai < 0){
//...
}elseif ($wai >= 0){
//...
}else{
//not reachable
return recursion($wa);
}
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.
UPDATE
You can test this condition for yourself
$tests = [
false,
-10,
1,
0,
[], ''
];
foreach($tests as $wai){
if ($wai < 0){
echo "1";
}elseif ($wai >= 0){
echo "2";
}else{
echo "3";
}
echo "\n";
}
Outputs
2
1
2
2
2
2
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:
$wai = $wa - $amounte;
global $wai;
Global must be declared first.
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();
Now one would expect this to print 50
, however it prints 100
. It executes this way:
- PHP parses and interprets the code
- We declare global
$wai
outside the function
- We set
$wai
to = 100 (assignment)
- We call function
foo
- We set the local variable
$wai
= 50;
- 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
- Then we echo that value.
This is a very close approximation of your code, it's functionally equivalent Check it yourself
Now if you change the order
function foo(){
global $wai;
$wai = 50;
echo $wai;
}
global $wai;
$wai = 100;
foo();
It outputs 50
test this
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);
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;
So
return "production code that should operate with different variations of $amounte and $redeem on last iteration";
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.