I have a PHP file "main.php" that has at the top the following statement:
include "config_file.php";
The config_file.php includes the settings of five simple variables. Is there a way to display or view these five variables in the main.php file? If not, is there a better way to define these variables outside the main.php file (in a config file) and yet see the variables in the main.php file? An example will help.
Thanks, Menachem
index.php
include 'config_file.php';
if(isset($db))
echo 'set';
else
echo 'not set'; // this will be called
config_file.php
$db = new mysqli("HOST", "USERNAME", "PASSWORD", "DB");
And Possible duplicate of How to access variable from an included page?
Edit 01
a.php
$a = 'Abulla';
$b = 'Menachem';
b.php
you can just use
include 'a.php';
echo $a.' and '.$b;
You can directly use the variable of included file
just
echo $var;
Including the PHP file usually will give you access to all variables defined in this file. That means that you can simply use variables defined in config_file.php
in your main.php
.
Example:
Main.php:
<?php
include "config_file.php";
// The code included below is example, you may do anything you want with variables
$example_result = $a + $b;
print($example_result); // Just and example
file_put_contents ("debug.txt" , $example_result); // Just an example
// This should result in debug.txt contain $a + $b meaning that this file content should be "3"
config_file.php:
<?php
$a = 1;
$b = 2;
This may slightly differ if one of your files is a PHP class. In this case please give us a notice.
The reason that I wanted to view the "include" file variables in the source code was to improve the readability of the code.
I solved this problem by pasting the content of the "include" file into the main file as comments.