0

I have a phonecode 1234 and i am checking this phonecode in my db to get its respective's country's name if matched i am returning country's name anf if not matched i am removing the last digit from phone code example 123 and checking again if matched i am returning the country name otherwise removing the last digit again example 12 until i reach 1.

function rFunc($val)
{
    if (strlen($val) != 0) {
        $sql = mysql_query("SELECT name from country Where phonecode='$val'LIMIT 1");
        if (mysql_num_rows($sql) > 0) {
            $res  = mysql_fetch_assoc($sql);
            $name = $res['name'];
            return $name;
        } else {
            rFunc(substr($val, 0, -1));
        }
    } else {
        return "N/A";
    }
}
$value = "1234";
$na    = rFunc($value);
Qirel
  • 25,449
  • 7
  • 45
  • 62
Benson OO
  • 477
  • 5
  • 22

1 Answers1

1

You just need to add return in your recursive function.

return rFunc(substr($val,0,-1));
Jaymin
  • 1,643
  • 1
  • 18
  • 39