My goal is to have a file with the following html/php created on the server automatically
which it does do, however it includes the php includes as well so when the page is build it includes everything that is in the include as well.
I would want include("../includes/right.html"); to be left alone
in the page that is generated,but I want other php variables to be parsed.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>
This is straight from the PHP manual at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
This is called nowdoc syntax and comes with PHP 5.3. It's generally the best practice for something like this.
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
anything between 'EOT' and EOT; will be rendered on the page as text with no parsing done whatsoever.
You need to escape that line as HTML, so it's not passed through the PHP parser, e.g.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>
Related
I trying to put user data from PHP in HTML form, like name, email, address, etc. But i don't know what i have to do to work it.
Already tried:
<?php
session_start();
$test='some name';
$_SESSION['name']='some name';
?>
<!-- AND -->
and nothing happened.
SOLVED CODE:
<?php
session_start();
$test='some name';
?>
<a><?= $test ?></a>
The PHP manual has a page introducing the PHP tags:
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them.
Note that "interpreting the code" is not the same as displaying the result of that code. For instance, you could write <?php $test = 'hello'; ?> to assign a new value to a variable, and it won't cause anything to be output.
To output a string, you can use the echo and print keywords. Note that these are not functions, so do not have parentheses around their arguments; as noted in the manual, including redundant parentheses will sometimes work, but can be misleading. So the following output the variable $test into the page:
<?php echo $test; ?>
<?php print $test; ?>
As the PHP tags page I linked to earlier says:
PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo.
So you can also use:
<?= $test ?>
However, you also have a second problem, which is nothing to do with PHP, and will happen if you hard-code a URL into your HTML like this:
Nothing will appear in the browser! Why? Because you've defined the destination of a link, but haven't defined any text or content to click on; you need it to look like this:
Click me if you dare!
So put that together, you might write this in PHP:
<?= $linkDescription ?>
Change this
to this
or to this if you want to see the data on the html page
<?php $test; ?>
<?php echo($test); ?>
Do note that there are better ways to display such data. Check this out:
https://www.w3schools.com/php/php_echo_print.asp
I am familiar with HEREDOC, but that is not what I want to use.
What I was hoping for was a way to edit HTML/CSS in the PHP file so that it is not technically a string so that I can get proper HTML/CSS/JS hints and autocompletes in my text editor. When trying to edit inside HEREDOC it is considered a string so I do not get any hits, which causes a lot of syntax errors.
Something like this: (Like the use of the if statement brackets)
<?php
$html='
?>
<style>
h4{
font-weight:700;
}
</style>
<h4>HTML</h4>
<?php
';
echo $html;
?>
Is there a way to put the html/css in another file then include it inside a concat string?
<?php
$html=''.include('html.php').'';
echo $html;
?>
You could use output buffering - in this simplified example you'd get the html from file view.php and you'd assign it to $html.
<?php
$html = getHtml('view.php');
echo $html;
function getHtml(string $file)
{
ob_start();
include $file;
return ob_get_clean();
}
ob_start() turns on output buffering
ob_get_clean() gets current buffer contents (returns as string) and deletes current output buffer
As far a PHP is concerned, HTML and JS is just text so you can store them in a PHP variable in the same way as you would store any string.
$html = '<style>
h4{
font-weight:700;
}
</style>
<h4>HTML</h4>';
To load HTML from a separate file into a variable, how you would do it depends on the file. If it contains PHP code that needs to be executed, then follow jibsteroos answer about output buffering.
If the file doesn't contain any PHP, you can use file_get_contents():
$html = file_get_contents('html.php');
Note: If html.php contains any PHP, it will be read as text instead of being executed.
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.
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 have a file which is combination of PHP and HTML.
How can i get the plain HTML code output from this file into a text file? or either put it into the text box.
The code of my form, which i use to generate an HTML form can be found here
Update:
It seems OP is asking about HOW to get the rendered PHP+HTML codes into a text file?
//get the output from the file
ob_start();
include "yourphpplushtml.php";
$output = ob_get_clean();
//now create a text file and write to it
$fp = fopen('data.txt', 'w+');
fwrite($fp, $output); //put the output
fclose($fp); //close the handler
//Or put it into the textare
echo '<textarea>'.$output.'</textarea>';
Previous Answer But may be helpful to others too
There are many ways HTML can be combined with PHP
Output HTML directly from PHP
<?php
echo "<head><title>my new title</title></head>";
?>
Include PHP inside you HTML
<title><?php echo $dynamictitle; ?></title>
Or even separate them in old fashioned complicated way
<?php if($resultFound == true) { ?>
<p> The result was successfully found.</p>
<?php } ?>