Search code examples
phpinclude

include file and get variables, but not content


I have a file which gets content from an XML.

ob_start();
require_once 'feed.php';

    $title = 'ExampleTitle';

    foreach(Feed('example.url') as $f ) {
        if (strpos($f->title, $title) !== false)
            echo $f->description;
    }

    $output = ob_get_contents();

Outputs example:

Currently 21*C and sunny.

I have saved the output in a variable using ob_start(); and $output = ob_get_contents();.

I want to use the $output and other variables inside another PHP file. but when I include it inside the other PHP using include ('status.php');. It outputs the result as well. I only want to access the variables, not the file to output anything.

I am currently using the file that outputs. So I don't want to change it

Hope it`s clear what I meant. Any help is greatly appreciated!


Solution

  • You can assign the variables in session, then you can use it in any page that you desire.

    i.e. inyour status.php

    <?php
    session_start();
    
    #your codes
    $output = ob_get_contents();
    
    $_SESSION["output"] = $output; //Then use the session
    
    ?>
    

    Or if you must include, then you will have to hide the dirt, in a hidden tag.

    <div style="display:none;">
    <?php  include('status.php'); ?>
    </div>
    
    <?php echo $output; //here is your variable  ?>