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

The file_get_contents() or include() reads a file into a variable. This function is the preferred way to read the contents of a file into a variable. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

1. Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//$data = file_get_contents("D:\soltuts.php");
$filePath = "D:\soltuts.php";
$data = include $filePath;
print_r($data);
?>
<?php //$data = file_get_contents("D:\soltuts.php"); $filePath = "D:\soltuts.php"; $data = include $filePath; print_r($data); ?>
 <?php
//$data = file_get_contents("D:\soltuts.php");
$filePath = "D:\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
<?php
return array(
'SOL' => 'Soltuts.com',
'SNIP' => 'Snipcode'
);
File: soltuts.php <?php return array( 'SOL' => 'Soltuts.com', 'SNIP' => 'Snipcode' );
File: soltuts.php 
<?php 
return array(
  'SOL' => 'Soltuts.com',
  'SNIP' => 'Snipcode'
);

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 )