How to load a php file into a variable in php?

In this post, we will show you how to use one PHP file’s variables in PHP.

1. Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
soltuts.php
<?php
return array(
'SOL' => 'Soltuts.com',
'SNIP' => 'Snipcode'
);
loadFile.php
<?php
$filePath = "soltuts.php";
$data = include $filePath;
print_r($data);
?>
soltuts.php <?php return array( 'SOL' => 'Soltuts.com', 'SNIP' => 'Snipcode' ); loadFile.php <?php $filePath = "soltuts.php"; $data = include $filePath; print_r($data); ?>
soltuts.php
<?php 
return array(
  'SOL' => 'Soltuts.com',
  'SNIP' => 'Snipcode'
);
loadFile.php
 <?php
$filePath = "soltuts.php";
$data = include $filePath;
print_r($data);
?> 

2. Example:

Input:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
file: soltuts.php
file: soltuts.php
file: soltuts.php

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Array ( [SOL] => Soltuts.com [SNIP] => Snipcode );
Array ( [SOL] => Soltuts.com [SNIP] => Snipcode );
Array ( [SOL] => Soltuts.com [SNIP] => Snipcode );