PHP:每页上的PHP自动加载变量

I have a constant

like define('SITE_URL','http://somewebsite.com');

I am using it on some pages

Some time also changing the value of that variable so that it is a pathetic task to go on each page and change the variable value .

Please suggest me some alternative like some automatic class loader or any other method .

Since it is my first PHP web so I am unable to decide what to do here ?

First of all a define is not a variable.

To use it in multiple pages, put all your defines in a separate PHP file, and include it in other php files when you want to use it.

example:

in defines.php:

define('SITE_URL','http://somewebsite.com');

in page1.php:

include 'defines.php';
// do stuff and use the define:
echo SITE_URL;  // <-- notice that there are no quotes

etc.

You can create a file with the variables, for example "variables.php", then on all the pages you want to use that variable you just do:

include("path_to_the_file/variable.php"); 

and you will have access to that variable, and you will able to change the value for that script.


If you want to change the value for all the scripts, use Session variables instead. You will need to have

session_start();

on all the pages which uses that variable. For setting the variable: $_SESSION['myVar'] = "value";

So anytime you need to access to that var you can"

echo $_SESSION['myVar'];

or you can change it's value:

$_SESSION['myVar'] = "new Value";

And it will be changed for the whole session.

remember to have session_start() so you can access it.