Where to declare variable that can be used throughout the entire script ?

Started by twerry, July 11, 2019, 11:16:27 AM

Previous topic - Next topic

twerry

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.

vbgamer45

Depends what type of variable?
You can stick it in settings.php

Then use it as a global

global $variablename;


echo $variablename;

Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Kindred

Or add it to the $context array -  or, if it is user specific, add it to the $user_info array
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

twerry

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.

twerry

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.

Kindred

You will still need to globalize the variable if you plan to use it anywhere outside of the specific header
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

twerry

Not if i include the variables.php file somewhere on top of any smf page that might be loaded

twerry

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.

Kindred

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.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

twerry

Indeed i overlooked i was working inside a function when including the variables file. Problem solved. Working perfect now. Thanks for your replies.

Advertisement: