SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Italy
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to define once a var???

    Hi,
    do anybody know if and how is possible to define only once a var?

    I mean that I will run the file xxx.php where I want to assign a value to the var $abc.
    After that I will run a lot of other php file, where I would like to have already assigned $abc, without do it in any php file.

    Thank you in advance
    Fausto

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Any time you include() or require() another PHP file, its contents are put into the scope at that statement as if it were all written right there. If $abc was already defined and available to that scope, it's available to all the code in the include()'d or require()'d file.

  3. #3
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can save the variable in cookie or session
    or if u need to make it to all members save it in file
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Italy
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you,
    so if I understood correctly:

    - I run the file a.php where I have include(x.php)
    - in x.php I assign $var=5;
    - then I run the file b.php (that not include x.php) and it will be already assigned the value $var.

    Correct?

    Thanks

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on what you mean by "run" a file.
    Is "run" the same as (please select one

    1. query the file from a webserver (http://server/script.php
    2. run from the command line (php -f script.php)
    3. include the file from within another file

  6. #6
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /* a.php 
    --------------------*/
    $myvar "abc";

    /* b.php
    --------------------*/
    include ('a.php');

    /* c.php
    --------------------*/
    include ('b.php');
    print 
    $myvar// will print "abc" 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •