![]() |
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 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 ![]() 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 ![]() 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 Thanks again! php function will echo but returned value is empty - El Forum - 06-07-2010 [eluser]mddd[/eluser] You're welcome. Goodnight ![]() 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(......); |