AbydosGater Posted November 13, 2007 Share Posted November 13, 2007 Hey guys, Been awhile since i have had time to do any php but this is driving me mad. I am setting two variables $comPost and $comAction.. Now if i echo these right after declaration, they work fine. But inside the displayForm() function, if i set them as global they will not echo, but if i set any other predefined variable as global they work. Code: <?php $args = explode('/', $params); $comPost = $args[0]; $comAction = $args[1]; echo "/".$comPost.":".$comAction."/"; // This echos correctly. function displayForm() { global $comPost, $comAction; echo "displayForm()"; echo "<br />"; echo "/".$comPost.":".$comAction."/"; //This doesnt show the variables? } displayForm(); ?> Anyone know why, even though the variables are global they dont work inside the function? Thanks for reading, Andy Link to comment https://forums.phpfreaks.com/topic/77233-simple-problem-with-variable-scope/ Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 I just tried your code and it works fine for me... Why don't you just pass the variables into the function, instead of using the global? <?php $args = explode('/', $params); $comPost = $args[0]; $comAction = $args[1]; echo "/".$comPost.":".$comAction."/"; // This echos correctly. function displayForm($comPost,$comAction) { echo "displayForm()"; echo "<br />"; echo "/".$comPost.":".$comAction."/"; //This doesnt show the variables? } displayForm(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/77233-simple-problem-with-variable-scope/#findComment-391019 Share on other sites More sharing options...
AbydosGater Posted November 13, 2007 Author Share Posted November 13, 2007 Yeah i was thinking about doing that, Just an easy work around, but i was still wondering why it wasnt declaring the variables global. It works with any other variable i have predefined.. Like if i declare $config as global i can echo that. But just before if i set $myVar = "test"; and global it inside the function it doesnt recognise it either. Find it odd. you say it works for you? Maybe something in the settings? Andy Link to comment https://forums.phpfreaks.com/topic/77233-simple-problem-with-variable-scope/#findComment-391026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.