adrianTNT Posted February 21, 2007 Share Posted February 21, 2007 Hello. I have a variable called $comments_per_page that is inside settings.php, ($comments_per_page = 10); Then a main file comments.php that contains: <?php // include settings.php that contains the value of $comments_per_page require settings.php; function some_function(){ // I need to use $comments_per_page here (inside a function) } // I also need to use $comments_per_page here (outside of the function) ?> I dont know how to access and use the variable $comments_per_page when inside or outside of a function, I know about 2 things but I am not sure how to use them: $GLOBALS['comments_per_page']; global $comments_per_page; - Which one of these 2 ^ I need to use inside and outside of a function when accessing and modifying $comments_per_page variable? - When I just have "$comments_per_page = 10" inside settings.php does that mean it was created as a global variable? Thank you. - Adrian. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 21, 2007 Share Posted February 21, 2007 Inside the function you need to use global $comments_per_page; before using it. That way you use the global instead of the local. Outside of the function I believe you can just use $comments_per_page. However, if you wish to be exactly sure about it, you can use $GLOBALS['comments_per_page']. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 You might consider setting $comments as a constant instead. That automatically makes it global. define('COMMENTS_PER_PAGE', 10); then in the function use it print COMMENTS_PER_PAGE; If you want to be able to change it that won't work, but as you said it's a setting I figure a constant will work better. Quote Link to comment Share on other sites More sharing options...
adrianTNT Posted February 21, 2007 Author Share Posted February 21, 2007 You might consider setting $comments as a constant instead. That automatically makes it global. define('COMMENTS_PER_PAGE', 10); then in the function use it print COMMENTS_PER_PAGE; If you want to be able to change it that won't work, but as you said it's a setting I figure a constant will work better. Yes, it is a setting and making it constant as you said could work, but I have more variables to use in same script and I was trying to figure out the way I have to use the other ones too. Inside the function you need to use global $comments_per_page; before using it. That way you use the global instead of the local. Outside of the function I believe you can just use $comments_per_page. However, if you wish to be exactly sure about it, you can use $GLOBALS['comments_per_page']. so if inside a function I add global $comments_per_page and after that I change it's value inside the same function does this mean the variable value is also changed globally? Or only temporary to be used inside that function? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 21, 2007 Share Posted February 21, 2007 It will be changed globally. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 AFAIK: It will be changed globally because it is a global variable. You might want to read up on passing by reference also. Quote Link to comment Share on other sites More sharing options...
adrianTNT Posted February 21, 2007 Author Share Posted February 21, 2007 It will be changed globally. Thanks, you are right. (I should have made a quick test for that). AFAIK: It will be changed globally because it is a global variable. You might want to read up on passing by reference also. I am not familiar with this 'passing by reference' I seen it again today at http://www.php.net/global but I dont realy understand what it is, I am kind of new to php I will try to use "global $variable_name" inside functions (now that I am sure what it does) and see if that fixes my issues. Thank you. Quote Link to comment Share on other sites More sharing options...
adrianTNT Posted February 21, 2007 Author Share Posted February 21, 2007 I got into an issue: when inside a function and use global $comments_per_page; I get: Parse error: parse error, unexpected T_GLOBAL Why could this happen ? edit: sorry about that, it was because of something else. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 It's probably the line before that. Quote Link to comment 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.