rahul.pache Posted April 10, 2008 Share Posted April 10, 2008 I am using XAMPP {PHP 5} Here is the code : <?php $var=0; function test() { echo "<br> VAr : " . $var; $var = 1; echo "<br> VAr : " . $var; } test(); if($var == 1) { echo "Hello"; } ?> I have taken 1 variable $var = 0; outside the function And I want to use it inside the function. So that after changing its value it will be available to print "hello" Plz suggest how to do that. I WILL BE VERY VERY THANKFULLLLLLL Link to comment https://forums.phpfreaks.com/topic/100452-very-basic-function-related-problem/ Share on other sites More sharing options...
friedemann_bach Posted April 10, 2008 Share Posted April 10, 2008 You might try this: <?php $var=0; function test() { $GLOBALS['var'] = 1; } test(); if($var == 1) { echo "Hello"; } ?> Link to comment https://forums.phpfreaks.com/topic/100452-very-basic-function-related-problem/#findComment-513691 Share on other sites More sharing options...
rahul.pache Posted April 10, 2008 Author Share Posted April 10, 2008 YES its working. Thanx BUT is it correct to use global variables ? Coz i have heard somewhere not to use global variables. And I dont know why ? Link to comment https://forums.phpfreaks.com/topic/100452-very-basic-function-related-problem/#findComment-513694 Share on other sites More sharing options...
friedemann_bach Posted April 10, 2008 Share Posted April 10, 2008 Depending on your needs, maybe this is a more elegant way: <?php $var = 0; function test($my_var) { $my_var = 1; } test(&$var); // a pointer at $var is given to your function if($var == 1) { echo "Hello"; } ?> Link to comment https://forums.phpfreaks.com/topic/100452-very-basic-function-related-problem/#findComment-513695 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.