I have had issues with a recursive function that has to check wether an id is already used in db table before storing the data. If it is, it has to times it by 1000. If it's still in use, it has to time it by 10 and so on.
I know we should use primary keys in our database, but this is the company policy.
Here is my code:
function checkIdExits($id_conf, $world, $index = 0) {
if ($index == 0) {
if (checkIdDB($id_conf, $world) === true) {
$id_conf = $id_conf * 1000;
//echo "times 1000 $id_conf";
} else {
return $id_conf;
}
} else {
$id_conf = $id_conf * 10;
//echo "times 10 $id_conf";
}
if (checkIdDB($id_conf, $world) === true) {
//echo "chek again $id_conf";
checkIdExits($id_conf, $world, 1);
} else {
echo $id_conf;
//return $id_conf;
}
}
checkIdDB
performes a query in the database and correctly returns true if there's a row, false if there are no rows.
All the echo
I put in the code show that the algorithm performs the correct operations.
However whenever it's run twice the return $id_conf;
line doesn't return anything.
I have tried using echo
instead of return
and that seems to work.
Can somebody explain why?