nzedcom nzedcom - 2 months ago 7
PHP Question

why is my function return on call not visible?

im dealing with encapsulated categories here. I want to resolve them until i reached the top layer by recursive function:

function resolve_kat($kat_id){

...credentials here...
$db = new mysqli($host_name, $user_name, $password, $database, NULL, $socket);

$sql_kat = "SELECT * FROM categories WHERE category=".$kat_id;
$kat_res = $db->query($sql_kat);
$kat = $kat_res->fetch_assoc();

if($kat['upppercategory']==0){
return $kat['category'];
}
else {
resolve_kat($kat['upppercategory']);
}

$kat_res->close();
}

$passtome = resolve_kat(927);

echo $passtome;


It works fine with
echo
, but i want to pass the value to an other function.
What did i wrong?

Answer

You look to be mising a return in your IF. Should be:

if($kat['upppercategory']==0){
   return $kat['category'];
}
else {
   return resolve_kat($kat['upppercategory']);
}

You can then pass it to another function.

$passtome = resolve_kat(927);

function otherFunction ($passtome) {
  #What you want to do...
}