Jump to content

Understanding global variables and how to define them, change them ...


adrianTNT

Recommended Posts

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.

Link to comment
Share on other sites

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'].

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.