PHP behaves strange when capturing 'return' value, how to over come? | Newbie | | Join Date: Aug 2008
Posts: 4
| |
PHP behaves strange when capturing 'return' value, how to over come?
when i echo the value inside the function, it works fine.
if i return a value from the function, it return nothing... why?
Assume, that the following is the table 'menu' and having the following fields(with values) -
menuid
-
======
-
1
-
2
-
3
-
4
-
5
-
parentmenuid
-
==========
-
-
1
-
1
-
5
-
1
-
//php code -
<?
-
include "db.php";
-
-
$returnval=trace_child(1,4);
-
echo $returnval;
-
-
function trace_child($parent,$child)
-
{
-
$result = mysql_query('SELECT * FROM menu
-
WHERE parentmenuid='.$parent);
-
-
while ($row = mysql_fetch_array($result))
-
{
-
if($child==$row['menuid'])
-
{
-
return true; ///the problem is here.. if i put echo 'true'; then it works fine... why
-
}
-
else{
-
trace_child($row['menuid'],$child);}
-
}
-
}
-
-
?>
-
i wants to trace whether menu '4' is grandchild of menu'1' .
please somebody help me...
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: PHP behaves strange when capturing 'return' value, how to over come?
Hi. Welcome to Bytes!
When you echo a Boolean value, you won't get "true" or "false". You will either get "1" if it is true, or simply nothing if it is false.
You need to use the value, like: -
if(functionCall()) {
-
echo "true";
-
}
-
else {
-
echo "false";
-
}
-
P.S.
Please use [code] tags when posting code examples.
| | Newbie | | Join Date: Aug 2008
Posts: 4
| | | re: PHP behaves strange when capturing 'return' value, how to over come? Quote:
Originally Posted by Atli Hi. Welcome to Bytes!
When you echo a Boolean value, you won't get "true" or "false". You will either get "1" if it is true, or simply nothing if it is false.
You need to use the value, like: -
if(functionCall()) {
-
echo "true";
-
}
-
else {
-
echo "false";
-
}
-
P.S.
Please use [code] tags when posting code examples.
Hi Mr. Atli,
i have tried that too.
still no use...
Actually, when we look at the above code it seems very fine...
But when we pass the function output to a variable i fails...( it is success when i use 'echo' instead of 'return')...
Mr.Atli,
Please do me a favour...
create a table(menu) that i mentioned previously, and also the field values...
now execute the above code(my php code)... Now u will know what i am trying to say...
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: PHP behaves strange when capturing 'return' value, how to over come?
Ahh ok now I see.
The problem is the recursive call in your function doesn't return it's value.
Consider this: -
function testFunc($value) {
-
if($value == 2) {
-
return true;
-
}
-
else {
-
++$value;
-
testFunc($value);
-
}
-
}
-
-
var_dump(testFunc(1));
-
# Prints: NULL
-
Now this function will check the value first, which is 1 in the first run. After the first if() fails, it increments and calls itself again. This time the value is 2, so it returns true.
The problem there is that the second call is the only one to return anything. You don't do anything with the results returned by that call (The recursive call, where the function calls itself).
As a result, the result is disregarded in the original call and the function returns NULL.
To fix this, in my example, you would have to add the return keyword to the second call: -
function testFunc($value) {
-
if($value == 2) {
-
return true;
-
}
-
else {
-
++$value;
-
return testFunc($value); # Change is here
-
}
-
}
-
-
var_dump(testFunc(1));
-
# Prints: bool(true)
-
In your function, you would have to check the return value of the recursive call. If it is true, return it. If it is not, continue the while loop and return false once the loop has ended.
| | Newbie | | Join Date: Aug 2008
Posts: 4
| | | re: PHP behaves strange when capturing 'return' value, how to over come? Quote:
Originally Posted by Atli Ahh ok now I see.
The problem is the recursive call in your function doesn't return it's value.
Consider this: -
function testFunc($value) {
-
if($value == 2) {
-
return true;
-
}
-
else {
-
++$value;
-
testFunc($value);
-
}
-
}
-
-
var_dump(testFunc(1));
-
# Prints: NULL
-
Now this function will check the value first, which is 1 in the first run. After the first if() fails, it increments and calls itself again. This time the value is 2, so it returns true.
The problem there is that the second call is the only one to return anything. You don't do anything with the results returned by that call (The recursive call, where the function calls itself).
As a result, the result is disregarded in the original call and the function returns NULL.
To fix this, in my example, you would have to add the return keyword to the second call: -
function testFunc($value) {
-
if($value == 2) {
-
return true;
-
}
-
else {
-
++$value;
-
return testFunc($value); # Change is here
-
}
-
}
-
-
var_dump(testFunc(1));
-
# Prints: bool(true)
-
In your function, you would have to check the return value of the recursive call. If it is true, return it. If it is not, continue the while loop and return false once the loop has ended. Hi Mr.Atli,
I was Very happy with your code when i when i got success for the above condition. But the program FAILS when it got morethan one child!
for example:
Condition: #2 (for Menu table) -
menuid(field1)
-
=====
-
1
-
2
-
3
-
4
-
5
-
-
parentmenuid(field2)
-
==========
-
-
1
-
2
-
2
-
1
-
now trace_child(2,4)
please check once again!
thanks for kind response!
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: PHP behaves strange when capturing 'return' value, how to over come?
What's your current code?
Also, if I may suggest and alternate solution to this problem.
Your current method of searching is somewhat wasteful.
Consider if you had one top-level menu, which had 100 child menus, each of which had a 100 child menus of their own.
If you were to try to match the root parent with the very last grandchild menu in that list, your search would have to go through every menu in the tree structure before it found a match.
Given our above data, that would take 9,999 queries!
My suggestion would be to search up from the child, looking for the parent, rather then your current method of searching down from the parent.
We don't usually build fully functional solutions for people, trying instead to help them create the solutions themselves.
But I found the best way to explain this properly would be to show you some code.
So, consider this. -
function findParent($parent, $child)
-
{
-
// Set the starting point for our search
-
$current = $child;
-
-
// Loop until the current menu is false.
-
// The root menu doesn't have a parent, so the parent of the root
-
// will be a null value (0), which will be evaluated as false, thus
-
// ending the loop.
-
while($current)
-
{
-
// Query for the parent of the current menu
-
$sql = "SELECT parent FROM menu WHERE child = $current";
-
$result = mysql_query($sql);
-
$row = mysql_fetch_assoc($result);
-
-
// Replace the current menu with it's parent
-
// so the search can continue from this point
-
// in the next loop
-
$current = $row['parent'];
-
-
// Check if the parent is the one we are looking for.
-
// If it is, the search is over and we return true
-
if($current == $parent) return true;
-
}
-
-
// The loop has ended without finding the parent.
-
// The search failed. Return false.
-
return false;
-
}
-
This function would only ever have to execute an amount of queries less than or equal to the depth of the tree structure. Which in your case is only 2.
| | Newbie | | Join Date: Aug 2008
Posts: 4
| | | re: PHP behaves strange when capturing 'return' value, how to over come? Quote:
Originally Posted by Atli What's your current code?
Also, if I may suggest and alternate solution to this problem.
Your current method of searching is somewhat wasteful.
Consider if you had one top-level menu, which had 100 child menus, each of which had a 100 child menus of their own.
If you were to try to match the root parent with the very last grandchild menu in that list, your search would have to go through every menu in the tree structure before it found a match.
Given our above data, that would take 9,999 queries!
My suggestion would be to search up from the child, looking for the parent, rather then your current method of searching down from the parent.
We don't usually build fully functional solutions for people, trying instead to help them create the solutions themselves.
But I found the best way to explain this properly would be to show you some code.
So, consider this. -
function findParent($parent, $child)
-
{
-
// Set the starting point for our search
-
$current = $child;
-
-
// Loop until the current menu is false.
-
// The root menu doesn't have a parent, so the parent of the root
-
// will be a null value (0), which will be evaluated as false, thus
-
// ending the loop.
-
while($current)
-
{
-
// Query for the parent of the current menu
-
$sql = "SELECT parent FROM menu WHERE child = $current";
-
$result = mysql_query($sql);
-
$row = mysql_fetch_assoc($result);
-
-
// Replace the current menu with it's parent
-
// so the search can continue from this point
-
// in the next loop
-
$current = $row['parent'];
-
-
// Check if the parent is the one we are looking for.
-
// If it is, the search is over and we return true
-
if($current == $parent) return true;
-
}
-
-
// The loop has ended without finding the parent.
-
// The search failed. Return false.
-
return false;
-
}
-
This function would only ever have to execute an amount of queries less than or equal to the depth of the tree structure. Which in your case is only 2. Mr. Atli,
Thanks a ton man. It works fine now...
thanks
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|