How to Execute and Get Content of a .PHP File in a Variable

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;

?>

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');

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 view file get the variable from controller?

Your require_once includes content of file in that place, so your view fiel can access any variables in same scope, so it can use passed variables from public function view($view, $data=[]) and also access $this.

In the end your code looks like this:

public function view($view, $data=[])
{
// after executing: require_once "./mvc/views/".$view.".php";
<h3><?php echo $data["sum"]; ?></h3>
}

Render PHP file into string variable

As other people have mentioned output buffering is probably the cleanest solution here, because it allows you to separate your html template away from your logic. This way you end up with fairly readable html in your template file, instead of a spaghetti code mess.

function render_php($path)
{
ob_start();
include($path);
$var=ob_get_contents();
ob_end_clean();
return $var;
}

Then create your template file

//test.php
<?php for($i = 0; $i<5; $i++):?>
<p><?php echo $i;?></p>
<?php endfor ?>

Then call your function:

render_php('test.php');

You could even make this more reusable by adding a second parameter (an array or object, i.e.

function render_php($path,array $args){
ob_start();
include($path);
$var=ob_get_contents();
ob_end_clean();
return $var;
}

Now lets see how this is useful

//create your template test.php
<?php for($i = $args['start']; $i<$args['end']; $i++):?>
<p><?php echo $i;?></p>
<?php endfor ?>

Now create your arguments and pass them off to the render method

$args = array('start' => 0, 'end' => 5);
render_php('test.php', $args);

Why This is Useful

Now you have a reusable function that is useful, no matter how many arguments you need to pass, and your logic can be in a separate file from your display, making your code a lot more readable. We could use this to build large chunks of html that is still easy to read.

i.e.

$article = array(    //imagine we have an article that we have pulled from our database
'title' => 'Some Title',
'subtitle' => 'Some Sub Title',
'body' => 'lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
eu nulla quis ligula ornare ultricies. Vivamus malesuada lectus a mi
auctor pellentesque. Maecenas eu ultricies sapien, ac porta augue. ',
'image' => 'img/some_image.jpg'
);

echo render_php('article.php',array $article);

and create a template

<!-- article.php -->
<!DOCTYPE html>
<html>
<head>
<title><?php echo $article['title']; ?></title>
</head>
<body>
<img src='<?php echo $article['image']; ?>' alt='whatever' >
<h1><?php echo $article['title']; ?></h1>
<h2><?php echo $article['subtitle']; ?></h2>
<p><?php echo $article['body'];?></p>

</body>
</html>

how do I get .php file variable to .py file

This above mentioned code is working absolutely fine but the twist is that the xyz.py file is running in background on command line terminal and this print(sys.argv) can use the variable value $para1 = "one"; in xyz.py file and print the value of that shared variable.
thanks

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.

php://input and file_get_contents just output the POST data rather than process it

A standard configuration of a web server is to execute PHP directives only in files with a .php file extension.

You could configure your web server to execute PHP in files with a .jpg file extension (the specifics depend on which web server you are using) but this would be highly unusual — doubly so because a JPEG image is a binary file and not a text file to start with.


Also note that allowing arbitrary PHP to be accepted as user input and then executed on your server is highly dangerous.



I'm aware I won't get executed when viewing the file but shouldn't the PHP I sent in the body get interpreted?

No. Reading a file into a variable only reads a file into a variable. file_get_contents does not execute PHP directives in user input.

That would also be highly dangerous and PHP isn't that bad.



Related Topics



Leave a reply



Submit