How do I load a PHP file into a variable?
I suppose you want to get the content generated by PHP, if so use:
$Vdata = file_get_contents('http://YOUR_HOST/YOUR/FILE.php');
Otherwise if you want to get the source code of the PHP file, it's the same as a .txt file:
$Vdata = file_get_contents('path/to/YOUR/FILE.php');
require/include into variable
I've also had this issue once, try something like
<?php
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
$test=requireToVar($test);
?>
PHP, getting variable from another php-file
You can, but the variable in your last include will overwrite the variable in your first one:
myfile.php
$var = 'test';
mysecondfile.php
$var = 'tester';
test.php
include 'myfile.php';
echo $var;
include 'mysecondfile.php';
echo $var;
Output:
test
tester
I suggest using different variable names.
Include file content in the variable
include
and require
are used to load PHP code into your application.
To get the content of a file and store it in a variable use file_get_contents
instead.
Do note that given path to the file is relative to the entry script. This might give unexpected results when your script is included from somewhere else. To counter this, use the __DIR__
magic constant.
Final code would look like:
$response['outputHTML'] = file_get_contents(__DIR__ . '/table-html.php');
If you want to parse PHP however
First of all, you might want to look at a templating engine like Twig, and save yourself some trouble.
If you want to do it yourself, the only thing you need to change in your code is to return a variable at the end of table-html.php
. I'd suggest using Heredoc to create readable and easy templating.
Example
<?php
return <<<EOT
<table>
<tr><td>$firstname</td><td>$lastname</td></tr>
</table>
EOT;
How to execute and get content of a .php file in a variable?
You can use the include directive to do this.
File 2:
<?php
$myvar="prashant";
?>
File 1:
<?php
include('myfile2.php');
echo $myvar;
?>
Load variables from external file in PHP
Look at this :
http://php.net/manual/en/function.parse-ini-file.php
you'll be happy :)
How to assign the contents of a file to a variable in PHP
If there is PHP code that needs to be executed, you do indeed need to use include
. However, include
will not return the output from the file; it will be emitted to the browser. You need to use a PHP feature called output buffering: this captures all the output sent by a script. You can then access and use this data:
ob_start(); // start capturing output
include('email_template.php'); // execute the file
$content = ob_get_contents(); // get the contents from the buffer
ob_end_clean(); // stop buffering and discard contents
How to use variables from php file where require/include is declared
It's very likely your functions don't work because their scope does not include the variables you are trying to use.
First, make sure functions.php
is being included after the variables are set
Also, make your functions Public Functions, OR, declare global variables inside functions by doing this:
$testVariable = "test";
function testFunction() {
global $testVariable;
}
Related Topics
Check If Current User Is Administrator in Wordpress
Send Post Data via Raw JSON with Postman
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
How to Show an Alert Box in PHP
Laravel Use Same Form for Create and Edit
Comparing Bcrypt Hash Between PHP and Nodejs
Quickest Way to Read First Line from File
PHP Method="Post" Stopped Working After I Added This .Htaccess... Why
Replace &Nbsp; Characters That Are Hidden in Text
Can't Find the File Created by Outfile in MySQL
Place PHP Results Inside HTML Page
Display Single Column Value of MySQLi Query
PHP Error: "The Zip Extension and Unzip Command Are Both Missing, Skipping."