How to declare variable in one php file and access in other php file

Discussion in 'PHP' started by globalcashsite, Jan 18, 2010.

  1. #1
    Guys

    I want to declare my site name in config.php so I can access this variable from all files like add.php. Please let me know how to do this without global_variable on. By default global_variable is off and I want to do this keeping it off?

    GCS
     
    globalcashsite, Jan 18, 2010 IP
  2. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #2
    You can declare ALL your variables in a 'my_vars.php' i.e. and then include this file into the rest of your files which require the variables :
    
    include ("path/to/file/my_vars.php");
    
    PHP:
    or
    
    require 'path/to/file/my_vars.php';
    
    PHP:
     
    dsignresponder, Jan 19, 2010 IP
  3. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi buddy

    Thank you for help, it is working.

    GCS
     
    globalcashsite, Jan 19, 2010 IP
  4. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #4
    You are welcome MAN! :)
    I'm glad to help!
     
    dsignresponder, Jan 19, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would suggest using CONSTANTS whenever possible if the values won't change, as they have global scope regardless
     
    JAY6390, Jan 19, 2010 IP
  6. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How to do that i.e. how to declare can you please give a sample command?

    GCS
     
    globalcashsite, Jan 19, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    JAY6390, Jan 19, 2010 IP
  8. kishore415

    kishore415 Well-Known Member

    Messages:
    1,462
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    160
    #8
    define("CONSTANT", "Hello world.");

    the above command is for declaring...and assigning value to it

    and the below is used for printing..

    echo CONSTANT; // outputs "Hello world."
     
    kishore415, Jan 19, 2010 IP
  9. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks a lot buddy. :) Is this variable in all script (different php files) automatically or I have to include this php file in all files like above?

    GCS
     
    globalcashsite, Jan 19, 2010 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    You can also use $_SESSION
     
    Kaizoku, Jan 21, 2010 IP
  11. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This may also useful.
    Declare ALL your variables in a 'my_vars.php' i.e. and then include this file into the rest of your files which require the variables :
    php Code:

    include ("path/to/file/my_vars.php");

    or
    php Code:

    require 'path/to/file/my_vars.php';
     
    lovingwings36, Jan 31, 2010 IP