jrg70 Posted July 14, 2006 Posted July 14, 2006 (edited) Hello all, I'm new to PHP scripting and I have a pretty basic question about returning a variable from within a user defined funciton. My question is: How do I do it? I've read through the PHP manual about returning values, but I can't seem to figure it out. As I said, I'm not a very knowledgable programmer, so I can't figure this out. Here is a basic script I've written with a funciton. ><script language="php"> // declare the function function test_function($var_01, $var_02) { $var_03 = $var_01 + $var_02; echo "$var_01 + $var_02 = $var_03 <br>"; return $var_03; } // declare starting variables $var_01=5; $var_02=6; // run the function test_function($var_01, $var_02); // this will output: 5 + 6 = 11 // output variable values echo "var_01 is: $var_01 <br>"; echo "var_02 is: $var_02 <br>"; echo "var_03 is: $var_03 <br>"; // this does not get output // Why is this variable not returned from the function? </script> How do I run the function and then have it return the value of $var_03? $var_03 is a valid variable from within the funciton, as it's value is output as 11. What am I doing wrong? Thanks, and please go easy on me Edited July 14, 2006 by jrg70 Quote
TCH-Bruce Posted July 14, 2006 Posted July 14, 2006 One way to do it is: >$variable = test_function($var_01, $var_02); Quote
jrg70 Posted July 14, 2006 Author Posted July 14, 2006 Thanks for the reply. But what I am not understanding is just what the "return" statement in front of the variable is doing. I would think that by placing that there it tells PHP to return that value to the caller and exit the function. It does exit the function, but the value does not seem to be returned. This is obviously just a test function. I would like to create functions that would be very useful in my scripts, that would return values. Quote
TCH-Dick Posted July 14, 2006 Posted July 14, 2006 You are trying to call a variable that is limited to the local scope of the function, in this case $var_03. You will have to assign a local variable to your function as in Bruce's example or rewrite the function with the varialble $var_03 being global. >function test_function($var_01, $var_02) { global $var_03; $var_03 = $var_01 + $var_02; echo "$var_01 + $var_02 = $var_03 <br>"; //Why is this here? return ($var_03); } Quote
jrg70 Posted July 14, 2006 Author Posted July 14, 2006 You are trying to call a variable that is limited to the local scope of the function, in this case $var_03. You will have to assign a local variable to your function as in Bruce's example or rewrite the function with the varialble $var_03 being global. >function test_function($var_01, $var_02) { global $var_03; $var_03 = $var_01 + $var_02; echo "$var_01 + $var_02 = $var_03 <br>"; //Why is this here? return ($var_03); } Thank you! That is exactly what I'm looking for. I didn't realize that you had to make the $var_03 global. I can now see the light. It looks like you put the comment on the code //Why is this here?. I put that there just to test if $var_03 had a value from within the function, which it did. Seriously, thank you. I knew this had to be something basic. Quote
TCH-Dick Posted July 14, 2006 Posted July 14, 2006 No problem and I see what you are doing with the echo now:) Quote
Steve Scrimpshire Posted July 15, 2006 Posted July 15, 2006 No offense to Dick, but it is a bad habit to get into declaring global variables inside functions. They are local by default for a good reason. If you alter the variable outside of the function, it could have unpredictable results. It is best to do it the way Bruce suggests. 'return' is useful for functions in several ways (testing for truth or falsehood is an example). >function test_function($function_var) { $test = $function_var % 9; return ($test); } $var_1 = 18; $answer = test_function($var_1); if ($answer){ echo "$var_1 is not evenly divisible by 9.\n"; } else { echo "$var_1 is evenly divisible by 9\n"; } The function just returns what the variable contains, not the variable itself, you need another variable to 'catch' what it returned. In the case of truth or falsehood, we could avoid having to use a variable to hold the returned value, by doing it directly in the 'if' statement: >if (test_function($var_1)){ echo "$var_1 is not evenly divisible by 9.\n"; } else { echo "$var_1 is evenly divisible by 9\n"; } 'else' is executed if test_function($var_1) returns 0 ('0' or 'null' [the variable never got assigned] are equal to 'false') and 'if' is executed if it returns anything besides '0' or 'null' (true). Notice the variable name used when defining the function: function test_function($function_var) Doesn't have to be the same as the variable used when calling the function: if (test_function($var_1)) The value of $var_1 is 'passed' to the function and the function catches it $function_var and uses it. $function_var is also 'local' to the function and will be null outside of the function. Hope this doesn't confuse you more. Quote
Steve Scrimpshire Posted July 15, 2006 Posted July 15, 2006 Notice the variable name used when defining the function:function test_function($function_var) Doesn't have to be the same as the variable used when calling the function: if (test_function($var_1)) As a matter of fact, it is a good idea to not name them the same thing, because you will get confused: >function my_function($var_1){ $var_1 = 13; echo "Inside the function \$var_1 equals $var_1\n"; return; } $var_1=11; my_function($var_1); echo "Outside the function \$var_1 still equals $var_1\n"; Outputs: >Inside the function $var_1 equals 13 Outside the function $var_1 still equals 11 Quote
TCH-Dick Posted July 15, 2006 Posted July 15, 2006 No offense to Dick, but it is a bad habit to get into declaring global variables inside functions. None taken as I was only addressing the scope of his variables. I agree with you about it being a bad habit but I'm sure he will figure that as he learns more. Quote
jrg70 Posted July 17, 2006 Author Posted July 17, 2006 Thanks for all the responses and help. I'm just starting out and this all helps. Thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.