I am trying to reach the contents of a PHP file without the file actually outputting what it would usually do. Here is my test code:
File1 (test1.php)
<?php
ob_start();
include_once './test2.php';
$test = ob_get_contents();
echo $test;
?>
and here is file2 (test2.php)
<?php
$testVar = 'Name!';
?>
<div class="testClass"><?php echo $testVar?></div>
<p>Spam2</p>
and I want it to only do this because of the
echo $test
line NOT because the file is outputting the content.
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
due to the echo, but it returns this
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
So how do I get it to only return the content once?
Don't echo $test;. PHP is executing as it should. Since ./test2.php shows Spam in HTML it appears on the page, then you assign the page contents to a variable and echo it. What do you expect?
If you have 2 files say: app/index.php and app/config.php you can just use the return keyword to return some content from the config.php file. And then, when you include the file whatever you returned from config.php can be saved to a variable.
Example:
First return whatever you want from the config.php file (could be an array, string, etc).
<?php
return ['name' => 'Spam'];
Then in the index.php:
<?php
$contents = include_once('config.php');
echo $contents;
Related
I put a sub file in the main file, but the sub file affects the variables in the main file.
Simple example:
In my code include can not be before echo.
Index.php
<?php
$heading= "Heading";
echo "<h1>$headeing</h1>";
include_once('specific_data.php');
?>
Specific_data.php
$heading = "Specific Heading";
Real output:
<h1>Heading</h1>
Desired output:
<h1>Specific Heading</h1>
You echo the value before include os before you assign the new value
try move the echo after the include
<?php
$heading= "Heading";
include_once('specific_data.php');
echo "<h1>$headeing</h1>";
?>
U can reate MVC stracture
For example
index.php
include_once 'view.php';
$heading='custom head';
return view('specific_data.php',compact('heading'));
view.php
function view($file,$vars=[])
{
extract($vars);
include_once $file.'.php';
}
specific_data.php
<h1>this variable get from index and will render <?=$heading?></h1>
Exec index.php from browser you will see following code
this variable get from index and will render custom head
01.php
<div class='title'>title</div>
02.php
<?php include('01.php');?>
some function:
$a = file_get_contents('02.php');
echo $a;
Result:
<?php include('01.php');?>
Is there a way to refer 02.php (without including it in the current file) and get this:
<div class='title'>title</div>
This is a simplified example. In reality 02.php is a large file with 01.php included somewhere inside.
If you want to parse the php file and save the result to a variable instead of outputting it you could use:
ob_start();
include("01.php");
$a = ob_get_contents();
ob_end_clean();
You will have the parsed php in $a but it will not be output to the user.
More info: http://php.net/manual/en/ref.outcontrol.php
I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.
This is odd... I want to include a file inside a variable to print it later but it's already including the file without printing the variable...
$var = include('file.php');
// stuff
echo $var;
file.php:
echo $stuff;
Output:
Notice: Undefined variable: stuff in file.php
I commented the echo $var to make sure but it's included anyways...
Is there any way to just load the content inside the variable instead of including the file?
try file_get_contents
<?php
$var = file_get_contents('file.php');
echo $var;
?>
You can do it like you did
$var = include 'file.php';
The included file needs to return a value, though.
Last in file.php:
// Do some stuff (but don't echo anything)
return 'This will be put in the var-variable to be echoed or used when you see fit';
How can I get expected output from example below?
Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.
content.php
<p>Hello <?php echo 'World!'; ?></p>
reader.php
<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>
Output of code above is:
Message from another file: Hello <?php echo 'World!'; ?>
Instead of (expected):
Message from another file: Hello World!
I think you are looking for <?php include('content.php');
file_get_contents — Reads entire file into a string
PHP.net file_get_contents - manual
The include statement includes and evaluates the specified file.
PHP.net include - manual
Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.
Example:
content.php
function get_content($world){
return '<p>Hello ' . $world . '</p>';
}
reader.php
<?php
require('content.php');
$content = get_content('world');
?>
<b>Message from another file:</b> <?php echo $content; ?>
Since you cannot use include (though I don't understand fully why), but want the file to be parsed and executed as PHP code, you can use eval
<b>Message from another file:</b> <?php eval(file_get_contents('content.php')); ?>
But the file content.php should not contain <?php and ?> tags, as stated at http://php.net/eval.