If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.
Related
I included an html file like this, so that it is not displayed when the site loads:
<div id="menugrp0" class="menuhide">
<?php include 'menugrp0.html'; ?>
</div>
Now I want it to be shown at a specific spot. I am using this php code, to get some variables which are transported with the $_SESSION. I am using this kind of question for some simple html links, in which case it works perfectly:
if ($_SESSION['gruppe'] == $h['gruppe']) {
printf(' menugrp0.html');
}
I know that this is not working at all at the moment for this included html. I also tried to add the <?php [...] ?> tag inside the printf, which is also not working.
Is it possible to show a hidden included html file with a printf tag?
Try this one.
<?php
if($_SESSION['gruppe'] == $h['gruppe']){
echo 'Foo';
include ('/path/to/menugrp0.html');
echo 'Example: one';
}
?>
readfile('menugrp0.html'); // Reads a file and writes it to the output buffer. It is like read then "echo"
How to echo the whole content of an .html file in php?
Thanks to Daan Meijer, this works.
echo file_get_contents('...');
}
Is it possible to include a php file without including its contents? I just want to access the functions and variables in that file without displaying any content. I tried this
<?
ob_start();
include('$file');
ob_end_clean();
?>
But this will hide only contents in php tag. I want to know how to hide others as well.
How to hide? Redesign your solution and separate your concerns! Do not mix logic with UI and so on.
Maybe you should apply the MVC or similar pattern(s).
While I totally agree with Peter's answer
I just tried this because I've never tried it before..
File toinclude.php:
<p>Loads of text</p>
<?php
function my_test()
{
echo 'Hello';
}
?>
Ooh a link
File includer.php:
<?php
ob_start();
include('toinclude.php');
ob_end_clean();
my_test();
?>
And it does work!
Output:
Hello
No you can't.
Include is meant to execute everything inside the file, there are no ways to prevent execution of some part of the file.
The only way is to edit the included file.
If your file is not printing any output You may try eval($fileContent).
Well most is in the title. I wonder if it's supposed to be that way or i can do the same without an if(1) condition I'm doing this because my website pages are all as php includes.
Thank you all
Answer retained:
Okay basically the way to do it is simply to include('file.php') as it will be considered out of the current <?php ?> environment.
Putting
<?php if(1): ?>
...
<?php endif; ?>
around your HTML code in a PHP file will have no effect on the result. You will still be able to include the file without it.
You can think of it like the "default mode" for a PHP file is that it contains HTML content. You only need to add <?php ?> tags if you want to add PHP code. If you're just putting HTML code in a PHP file, they're unnecessary.
The beauty of PHP is that you can move "in" and "out" of PHP very easily. You can do the following without issues:
<?PHP
if(whatever) {
?>
your HTML
<?php
include('whatever.php');
?>
more HTML
<?PHP
}
?>
To build on Zak's answer:
You can also use PHP to echo out things that aren't PHP... as long as you quote it appropriately.
<?php
//HTML
while ($x < 5) {
echo "<p> this is html that you can wrap with html tags! </p>";
$x++;
}
//Javascript
echo "<script type='text/javascript'>
some javascript code
</script>"
?>
Although, it's less confusing to just end the php tag to keep things separate.
And you can even use php as you want within html or javascript as long as you put the tags, and as long as the file is saved as a .php file (so PHP can be processed on the server).
Ex:
<script type="text/javascript">
//set a javascript image array to a php value
var imgArray = [<?php echo implode(',', getImages()) ?>];
</script>
But if you want to do this the other way around (IE, assign a browser-compiled value, such as a javascript value to a php value), you'll need to use AJAX.
I've got a simple (but not tiny) template for some HTML, complete with inline variables. I'd like to pull that out as a separate file, and have the ability to switch in other template files. Is there a way to load a file into a string, but have it process inline variables?
Eg:
$thing="complete sentence";
$test=<<<END
This will get parsed as a $thing.
END;
echo $test; // This will get parsed as a complete sentence.
What I want is something like this:
// "test.html"
<html>
<body>
<p>This will get parsed as a $thing.</p>
</body>
// "index.php"
$thing="complete sentence";
$test=file_get_contents("test.html");
echo $test; // This will get parsed as a complete sentence.
How do I achieve this, preferably without a templating library?
<?php
$thing="complete sentence";
$test=file_get_contents("test.php");
echo preg_replace_callback('#\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)#','changeVariables',$test);
function changeVariables($matches)
{
return $GLOBALS[$matches[1]];
}
This code uses preg_replace_callback to check what is variable. But, because we are in function, we cannot directly access script variables. We have to use $_GLOBALS variable which contains every script variable. $matches[1] contains name of matched variable.
Something like this should work...
// "test.php"
This will get parsed as a %s.
// "index.php"
$thing="complete sentence";
$test=file_get_contents("test.php");
printf($test, $thing);
You can use include to simply load the file as if it were part of the calling code.
include("included_file.php");
If you cannot include for some reason, you can read the file contents and eval it.
$content = file_get_contents("included_file.php");
eval($content);
UPDATE:
As pointed by NikiC, your file test.html doesn't have valid PHP. You would have to change it so include can work. Your test.html should have this content:
<html>
<body>
<p>This will get parsed as a <?= $thing ?>.</p>
</body>
And eval would not work with this code, as this is not pure PHP code, it is HTML code with PHP inside it. If your included file has just PHP code, it would work fine.
Is it possible for PHP file to print itself, for example <?php some code; ?> that I get output in HTML as <?php some code; ?>(I know its possible in c++), if not is it possible to actually print html version of php code with nice formatting and colors such as from this url inside code container http://woork.blogspot.com/2009/07/twitter-api-how-to-create-stream-of.html. OR from this website when you press code, while posting your example your code gets wrapped or whatever term is for that, makes it distinguishable from other non-code text. tnx
Yes.
<?php readfile(__FILE__)
__FILE__ is a magic constant that contains the absolute filesystem path to the file it is used in. And readfile just reads and prints the contents. And if you want to have a syntax highlighted HTML output, try the highlight_file function or highlight_string function instead.
I'm not sure if this is exactly what you want but you can print a file using:
echo file_get_contents(__FILE__);
or syntax-highlighted:
highlight_file(__FILE__);