anevins Posted July 4, 2011 Share Posted July 4, 2011 in the following code snippet, how does variable 'a' hold value of 1? I don't understand how that 1 gets in that variable. function test($a){ $c = 2; echo $c; echo $a; return 6; } $c = 1; echo $c; echo test($c); echo $c; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/ Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 somewhere when the functions is called ie: test(55); $a will be 55 test(65); $a will be 65 test(75); $a will be 75 test(99); $a will be 99 Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238159 Share on other sites More sharing options...
anevins Posted July 4, 2011 Author Share Posted July 4, 2011 In my example, I don't see an integer in parenthesis next to the function, so I can't make sense of it that way. Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238166 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 Ahh didn't pay mind to the whole thing.. Take note to your setting of the $c= variable 1 one.. then 3 lines down.. echo test($c); $c by the time it reaches the call to the function is defined as 1, so it is being passed off to the function as 1.. function test($a).. $a is contained to the function itself its a static variable you can use within the function itself. Lets try this.. function monkey($tooth){ if(!is_numeric($tooth)){$output = "Tooth is not Numeric, please enter a number and try again";} else{ $output = $tooth+20; //taking the numeric value of $tooth and adding 20 to it.. } return $output; } echo monkey(80); echo "<br />"; echo monkey("abc"); Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238172 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 Another thing worth mentioning exit(); Not the best way to end your scripts especially if your going to include scripts within scripts via include(), include_once(), require(), require_once().. exit will just halt your scripts rending from the second its executed so any lines there after included via other files or within the same script will not render. Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238173 Share on other sites More sharing options...
anevins Posted July 4, 2011 Author Share Posted July 4, 2011 ok what does $a have to do with $c? I can see how by the time I reach the function, $c has the value of 1 so therefore $a has value of 1? I just dont know how that links up, why arent they different? Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238181 Share on other sites More sharing options...
anevins Posted July 4, 2011 Author Share Posted July 4, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238244 Share on other sites More sharing options...
TeNDoLLA Posted July 4, 2011 Share Posted July 4, 2011 Hope this explanation helps... // Define function with parameter function test($thisIsParameter1) { // This is the parameter one that u pass to the function, // dosnt matter what the parameter is called outside the function // Inside the function it will be $thisIsParameter1. echo $thisIsParameter1; // will echo out 123 } $abc = '123'; test($abc) // Use function and pass one parameter $abc to it Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238250 Share on other sites More sharing options...
jcbones Posted July 4, 2011 Share Posted July 4, 2011 Variable Scope in PHP Quote Link to comment https://forums.phpfreaks.com/topic/241054-quick-query-for-php/#findComment-1238267 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.