Get variables from another php file without 'including' it?

Discussion in 'PHP' started by Kerosene, Mar 14, 2008.

  1. #1
    I need to grab some variables from a php file without including the rest of the content - is this possible?

    Example:
    I'd like to be able to get $var1 from myvars.php without having the echo displayed on my page. Can this be done?

    myvars.php:
    $var1 = "abc";
    echo "blablabla";
     
    Kerosene, Mar 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    ob_start();
    include 'myvars.php';
    ob_end_clean();
    
    echo $var1;
    
    PHP:
     
    nico_swd, Mar 14, 2008 IP
    Kerosene likes this.
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    Rockin'
    Thanks :)
     
    Kerosene, Mar 14, 2008 IP
  4. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #4
    doesnt it still include the whole file ?
     
    PowerExtreme, Mar 14, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Yes, but he just wants to prevent the output from the file... which is what my code does.
     
    nico_swd, Mar 14, 2008 IP
  6. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #6
    oh ok got it i though something else
     
    PowerExtreme, Mar 14, 2008 IP
  7. ProxyRadar

    ProxyRadar Greenhorn

    Messages:
    22
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    20
    #7
    May be it will be a good idea to store these variables in config file,
    and then parse it to an array by parse_ini_file function?
     
    ProxyRadar, Sep 18, 2015 IP
    deathshadow and ThePHPMaster like this.
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    That or in sessions... or in a database... killing the output by buffering is a massive waste of memory and processor time.
     
    deathshadow, Sep 19, 2015 IP