0

OK, this is making no sense and is the first time i have had this problem.

Please tell me i am being stupid or something.

Here is my function:

function getCountry($n, $origCode)
{
    global $countryData;
    if(strlen($n) > 0)
    {
        if(isset($countryData[$n]))
        {
            //$return = $countryData[$n];
            var_dump($n);
            return $n;
        }
        else
        {
            $n = substr($n, 0, -1);
            getCountry($n, $origCode);
        } 
    }
    else
    {
        echo "ERROR exiting couldn't find code $origCode $count<br>";   
    }

}

This is the call

foreach($file as $line)
{
    $split = explode(",", $line);
    echo "using $split[1]<br>";
    $country = getCountry(trim($split[1]), trim($split[1]));
    var_dump($country);
    echo "<br>";
}

File array:

$file = array("AA,93",
                "BB,9370",
                "CC,9378",
                "DD,9377",
                "EE,937",
                "FF,9379",
                "GG,355",
                "HH,35568",
                "II,35567"
            );

Country data array is

array('93'=>array('id'=>2')
    '355'=> array('id'=>'3)
);

The var_dumps are:

using 93 
string '93' (length=2)
string '93' (length=2)

using 9370 
string '93' (length=2)
null

using 9378 
string '93' (length=2)
null

using 9377 
string '93' (length=2)
null

using 937 
string '93' (length=2)
null

using 9379 
string '93' (length=2)
null

using 355 
string '355' (length=3)
string '355' (length=3)

using 35568 
string '355' (length=3)
null

using 35567 
string '355' (length=3)
null

What i dont understand at all is why when i var_dump just before i return the value it is ok but once a var_dump out of the function i get null

This is confusing me and i have no idea.

Any ideas??

Regards

Liam

3
  • 3
    At first glance, I believe you're missing a return on your call to getCountry in the else ... it should be return getCountry($n, $origCode); Commented Oct 24, 2013 at 14:50
  • in the first else the function doesn't return anything Commented Oct 24, 2013 at 14:51
  • dleiftah - I feel like an idiot haha. Many thanks it was something that simple. Commented Oct 24, 2013 at 14:51

1 Answer 1

2

Because you don't return the value, when you call your function recursive you should return the result of your recursivelly called function.

In your else condition, you should return:

    else
    {
        $n = substr($n, 0, -1);
        return getCountry($n, $origCode);
    } 

And your entire function became:

function getCountry($n, $origCode)
{
    global $countryData;
    if(strlen($n) > 0)
    {
        if(isset($countryData[$n]))
        {
            //$return = $countryData[$n];
            var_dump($n);
            return $n;
        }
        else
        {
            $n = substr($n, 0, -1);
            return getCountry($n, $origCode);
        } 
    }
    else
    {
        echo "ERROR exiting couldn't find code $origCode $count<br>";   
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.