0

I have 2 PHP documents; my main index.php and my data.php which gets some info from a database and puts it into variables. How would I echo some variables from the data.php in the index.php?

4
  • use session or get/post the data from one page to another Commented Oct 13, 2014 at 10:59
  • 1
    @SteamPunk_Devil Are you executing index.php? - I guess. Then include data.php in index.php document and set some variables. And just print them in index.php. Commented Oct 13, 2014 at 11:00
  • @SteamPunk_Devil put some more details please in case I did not get you. Commented Oct 13, 2014 at 11:00
  • @PHPWeblineindia How would I execute data.php from index.php? Commented Oct 13, 2014 at 11:03

6 Answers 6

2

@SteamPunk_Devil

Suppose below is your index.php file:

<?php

    // This is index.php

    // Your initial code .....

    require 'data.php'; // in data.php set some variables, arrays etc.

    // Now you can print the variables, arrays from data.php file in index.php    

?>

Let me know if this does not make sense.

Sign up to request clarification or add additional context in comments.

2 Comments

@SteamPunk_Devil Also check out this
Thanks thats exactly what I wanted
0

If you want really different files. You have to save your data in a session or in your database.

Or otherway if you redirect from one to another send your data over GET or POST.

Comments

0

You can use the global keyword in front of the variable you want to use, in the file you wish to use it to tell your files that something should be available in all your files, so long as the file is being included.

http://php.net/manual/en/language.variables.scope.php

Comments

0

In the index.php

$variable = 'value';//Initialize before including

require_once "data.php";

In the data.php

echo $variable;

A better approach is having a file containing all variable initializations which are needed in all the files in the script execution and then requiring this file before the files which are going to use the variables

Comments

0

A better solution is to make function that obtain data in data.php, and include this file to index.php with executing this function, f.e.:

file data.php:

 <?php
        function getData()
        {
            return 'data';
        }

file index.php:

<?php
    require_once 'data.php';
    $data = getData();
    echo $data;

Comments

0

you can use include() ,require_once() or require() for include data.php in index.php file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.