CodeIgniter Forums
php function will echo but returned value is empty - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: php function will echo but returned value is empty (/showthread.php?tid=31109)



php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]skunkbad[/eluser]
I'm working with a function that is just plain php. The function creates the string that I am looking for, so it does work, but it won't return the string. Code below shows echo $path, but if I return $path, the string should be output by var_dump(). Anyone see the problem why the return doesn't work?

Code:
<?php

$all_categories_array = array(
    '0' => array(
                'category_id' => '1',
                'category_name' => 'Examples',
                'parent_id' => '0'
    ),
    '1' => array(
                'category_id' => '2',
                'category_name' => 'Widgets',
                'parent_id' => '1'
    ),
    '2' => array(
                'category_id' => '3',
                'category_name' => 'Little',
                'parent_id' => '1'
    ),
    '3' => array(
                'category_id' => '4',
                'category_name' => 'Hardcore',
                'parent_id' => '2'
    ),
);


function get_product_parents($id, $all_categories_array, $steps)
{
    // check the entire categories array for a matching category
    foreach($all_categories_array as $x => $y)
    {
        // if this is the product's category
        if($y['category_id'] == $id)
        {
            // add the category name to the steps array
            $steps[] = $y['category_name'];

            // and do recursion if not a top level category
            if($y['parent_id'] !== '0')
            {
                get_product_parents($y['parent_id'], $all_categories_array, $steps );
                exit;
            }
            else
            {
                $reverse_array = array_reverse($steps);
                $path = '';
                foreach($reverse_array as $category_name)
                {
                    $path .= '/' . $category_name;
                }

                // Should be /Examples/Widgets/Hardcore
                echo $path;
            }
        }
    }

}

$steps = array();
$whatever = get_product_parents('4', $all_categories_array, $steps);
var_dump($whatever);



php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]mddd[/eluser]
Quote:Anyone see the problem why the return doesn’t work?
Because you are not returning anything? There is no return command anywhere in your code.


php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]skunkbad[/eluser]
if you read what I was saying, it says that if I use return instead of echo, it is empty. So substitute return for echo, and it should be output by var_dump(), but it's not.


php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]mddd[/eluser]
Allright, I'm sorry, I read your post and thought you meant 'i see it in the echo statement but it doesn't return anything'. Which would be true if you don't return anything Smile

I looked again at your code. I think the problem is this: if you exchange the 'echo' for 'return' that will return the value. But only to the function that called it. You are dealing with a recursive function so the previous iteration must also return a value to ITS caller, and so on until you get to the first call, which is in your "main code". See what I mean? Right now, the end value is known only in the last call to the function, which is the one where you find 'parent==0'. But then the value has to go back all the way to the first call. So you need to return a value on every place where the function could end. Not only when parent==0 but also when it is not: that is where you are now writing exit();


php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]skunkbad[/eluser]
[quote author="mddd" date="1275918748"]Allright, I'm sorry, I read your post and thought you meant 'i see it in the echo statement but it doesn't return anything'. Which would be true if you don't return anything Smile

I looked again at your code. I think the problem is this: if you exchange the 'echo' for 'return' that will return the value. But only to the function that called it. You are dealing with a recursive function so the previous iteration must also return a value to ITS caller, and so on until you get to the first call, which is in your "main code". See what I mean? Right now, the end value is known only in the last call to the function, which is the one where you find 'parent==0'. But then the value has to go back all the way to the first call. So you need to return a value on every place where the function could end. Not only when parent==0 but also when it is not: that is where you are now writing exit();[/quote]

OK, thanks, I got it working. I didn't understand that I would have to return all the way back through the recursion. I don't seem to remember doing that before, at least not in a CI function... but this is just a plain php function, so maybe it's different?

Anyways, this is the code:
Code:
<?php

$all_categories_array = array(
    '0' => array(
                'category_id' => '1',
                'category_name' => 'Examples',
                'parent_id' => '0'
    ),
    '1' => array(
                'category_id' => '2',
                'category_name' => 'Widgets',
                'parent_id' => '1'
    ),
    '2' => array(
                'category_id' => '3',
                'category_name' => 'Little',
                'parent_id' => '1'
    ),
    '3' => array(
                'category_id' => '4',
                'category_name' => 'Hardcore',
                'parent_id' => '2'
    ),
);


function get_product_parents($id, $cats_array, $steps)
{
    // check the entire categories array for a matching category
    foreach($cats_array as $x => $y)
    {
        // if this is the product's category
        if($y['category_id'] == $id)
        {
            // add the category name to the steps array
            $steps[] = $y['category_name'];

            // and do recursion if not a top level category
            if($y['parent_id'] !== '0')
            {
                return get_product_parents($y['parent_id'], $cats_array, $steps );
            }
            else
            {
                $reverse_array = array_reverse($steps);
                $path = '';
                foreach($reverse_array as $category_name)
                {
                    $path .= '/' . $category_name;
                }

                // Should be /Examples/Widgets/Hardcore
                $final_path = $path;
            }
        }
    }

    if(isset($final_path))
    {
        return $final_path;
    }
}

$steps = array();
$whatever = get_product_parents('3', $all_categories_array, $steps);
var_dump($whatever);
Now I'm going to bed, because it's 2:02am here!
Thanks again!


php function will echo but returned value is empty - El Forum - 06-07-2010

[eluser]mddd[/eluser]
You're welcome. Goodnight Smile

As for the CI / not CI: if you would save the value somehow, then you are right, you wouldn't have to return it.
For example: if you wrote '$this->path = $path;' in stead of your 'echo', then you could just use
Code:
get_product_parents(......);
// and now $this->path would contain the right value