vivis933 Posted September 9, 2016 Share Posted September 9, 2016 It is possible to make it like the example below ? function funct1($number1, $number2) { $variable1 = $number1 + $number2 ; } function funct2($variable1 , $number3) { $variable2 = $variable1 / $number3 ; } Quote Link to comment https://forums.phpfreaks.com/topic/302119-access-variable-inside-function-from-another-function/ Share on other sites More sharing options...
requinix Posted September 9, 2016 Share Posted September 9, 2016 The code is syntactically valid. If I knew what you were trying to do with it, I would make a comment pertaining to that. Quote Link to comment https://forums.phpfreaks.com/topic/302119-access-variable-inside-function-from-another-function/#findComment-1537220 Share on other sites More sharing options...
vivis933 Posted September 9, 2016 Author Share Posted September 9, 2016 (edited) This an example of what i need , basically the variable from first function to be included in the second function. <?php $number1 = 6 ; $number2 = 4 ; $number3 = 2 ; funct1($number1, $number2); funct2($variable1 , $number3); echo $variable2; function funct1($number1, $number2) { $variable1 = $number1 + $number2 ; } function funct2($variable1 , $number3) { $variable2 = $variable1 / $number3 ; } ?> ERROR PHP Notice: Undefined variable: variable1 on line 6 PHP Notice: Undefined variable: variable2 on line 7 Edited September 9, 2016 by vivis933 Quote Link to comment https://forums.phpfreaks.com/topic/302119-access-variable-inside-function-from-another-function/#findComment-1537222 Share on other sites More sharing options...
Jacques1 Posted September 9, 2016 Share Posted September 9, 2016 Well, this is not how variables work. A local variable is only visible within that function and gets destroyed immediately when the function returns. I think what you actually want to do is return the result of the addition so that you can then use it outside of the function: <?php function add($number_1, $number_2) { return $number_1 + $number_2; // add the two numbers and return the result } function div($number_1, $number_2) { return $number_1 / $number_2; } $sum = add(6, 4); $quot = div($sum, 2); echo $quot; 1 Quote Link to comment https://forums.phpfreaks.com/topic/302119-access-variable-inside-function-from-another-function/#findComment-1537227 Share on other sites More sharing options...
vivis933 Posted September 9, 2016 Author Share Posted September 9, 2016 Thank you for the effort i will try Quote Link to comment https://forums.phpfreaks.com/topic/302119-access-variable-inside-function-from-another-function/#findComment-1537230 Share on other sites More sharing options...
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.