AV1611 Posted September 25, 2007 Share Posted September 25, 2007 If I create a $var $myvar="some string"; then I make a function: function myfunction();{ ... }; what do I have to do to be able to use $myvar from within the function? Quote Link to comment https://forums.phpfreaks.com/topic/70554-solved-var-in-function/ Share on other sites More sharing options...
AV1611 Posted September 25, 2007 Author Share Posted September 25, 2007 Never mind global $myvar; Quote Link to comment https://forums.phpfreaks.com/topic/70554-solved-var-in-function/#findComment-354523 Share on other sites More sharing options...
pocobueno1388 Posted September 25, 2007 Share Posted September 25, 2007 Pass it as a parameter of the function. <?php function myFunc($var){ echo $var; } $str = "Blaaaaah"; myFunc($str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70554-solved-var-in-function/#findComment-354525 Share on other sites More sharing options...
trq Posted September 25, 2007 Share Posted September 25, 2007 Globals are bad. <?php $myvar = "some string"; function myfunction($v) { echo $v; } myfunction($myvar); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70554-solved-var-in-function/#findComment-354528 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.