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?
6 Answers
@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.
2 Comments
@SteamPunk_Devil Also check out this
SteamPunk_Devil
Thanks thats exactly what I wanted
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.
Comments
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
sessionorget/postthe data from one page to another