Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: twerry on July 11, 2019, 11:16:27 AM

Title: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 11:16:27 AM
I would think this is to be declared somewhere in index.php in the main forum directory ? But i have tried it and it does not work.

Lets say i want to use the variable $somevar = 'somevalue'; in Boardindex.php and somewhere else in index.template.php and maybe later as well at other places ...

I want to add my own variable that can be called upon anywhere in the script.

Thanks in advance for helping me out.
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: vbgamer45 on July 11, 2019, 11:31:19 AM
Depends what type of variable?
You can stick it in settings.php

Then use it as a global

global $variablename;


echo $variablename;

Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: Kindred on July 11, 2019, 11:38:50 AM
Or add it to the $context array -  or, if it is user specific, add it to the $user_info array
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 11:44:54 AM
Its a variable that i also use in the non smf parts of the website. So just a normal variable please. No global. No array. Just a casual variable containing a simple string.
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 11:56:05 AM
I ll be a little more specific maybe

In my own script of the website where smf forum is part of i have this header.php file.
The file loads no matter what page you are visiting.

In this file i have this written on top of it  ...

include 'includes/variables.php';

In the variables.php file are some variables declared that can be called upon on any page.

Now i want to be able to use that same variables inside the smf script. This means i am looking for the part where i must ad include '../includes/variables.php; so that i can call upon the same variables also inside the smf script.
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: Kindred on July 11, 2019, 11:57:26 AM
You will still need to globalize the variable if you plan to use it anywhere outside of the specific header
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 12:23:19 PM
Not if i include the variables.php file somewhere on top of any smf page that might be loaded
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 12:44:31 PM
Is there no place in the smf script that gets loaded no matter what page is builth/requested/loaded ? Because that is the only information i actually need.
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: Kindred on July 11, 2019, 01:42:22 PM
Yes, even if you include the variable.php file...

If you plan to use the variables inside of SMF functions, they will need to be globalized.
Title: Re: Where to declare variable that can be used throughout the entire script ?
Post by: twerry on July 11, 2019, 06:53:20 PM
Indeed i overlooked i was working inside a function when including the variables file. Problem solved. Working perfect now. Thanks for your replies.