Advertisement:

Author Topic: Where to declare variable that can be used throughout the entire script ?  (Read 1001 times)

Offline twerry

  • Newbie
  • *
  • Posts: 8
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.

Online vbgamer45

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 21,559
    • smfhacks on Facebook
    • VBGAMER45 on GitHub
    • @createaforum on Twitter
    • SMF For Free
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

Offline Kindred

  • The Mean One
  • Support Specialist
  • SMF Legend
  • *
  • Posts: 58,502
  • Gender: Male
    • Kindred-999 on GitHub
Or add it to the $context array -  or, if it is user specific, add it to the $user_info array
Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

Offline twerry

  • Newbie
  • *
  • Posts: 8
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.

Offline twerry

  • Newbie
  • *
  • Posts: 8
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.

Offline Kindred

  • The Mean One
  • Support Specialist
  • SMF Legend
  • *
  • Posts: 58,502
  • Gender: Male
    • Kindred-999 on GitHub
You will still need to globalize the variable if you plan to use it anywhere outside of the specific header
Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

Offline twerry

  • Newbie
  • *
  • Posts: 8
Not if i include the variables.php file somewhere on top of any smf page that might be loaded

Offline twerry

  • Newbie
  • *
  • Posts: 8
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.

Offline Kindred

  • The Mean One
  • Support Specialist
  • SMF Legend
  • *
  • Posts: 58,502
  • Gender: Male
    • Kindred-999 on GitHub
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.
Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

Offline twerry

  • Newbie
  • *
  • Posts: 8
Indeed i overlooked i was working inside a function when including the variables file. Problem solved. Working perfect now. Thanks for your replies.