I am new to php and wondering if I can have something like this:
<?php
...
magicFunctionStart();
?>
<html>
<head>...</head>
<body>...</body>
</html>
<?php
$variable = magicFunctionEnd();
...
?>
What I have to use right now is
<?php
...
$variable = "<html><head>...</head><body>...</body></html>"
?>
Which is annoying and not readable.
Have you tried "output buffering"?
<?php
...
ob_start();
?>
<html>
<head>...</head>
<body>...<?php echo $another_variable ?></body>
</html>
<?php
$variable = ob_get_clean();
...
?>
I think you want heredoc syntax.
For example:
$var = <<<HTML
<html>
<head>
random crap here
</html>
HTML;
I'm not really sure about what you are trying to accomplish, but I think something like the heredoc syntax might be useful for you:
<?
$variable = <<< MYSTRING
<html>
<head>...</head>
<body>...</body>
</html>
MYSTRING;
However if you are trying to make HTML templates I would highly recommend you to get a real templating engine, like Smarty, Dwoo or Savant.
Ok what you want to do is possible in a fashion.
You cannot simply assign a block of HTML to a php variable or do so with a function. However there is a number of ways to get the result you wish.
Investigate the use of a templating engine (I suggest you do this as it is worth while anyway). I use smarty, but there are many others
The second is to use an output buffer.
One of the problems you have is that any HTML you have in your page is immediately sent to the client which means it cant be used as a variable in php. However if you use the functions
ob_start and ob_end_fush you can achive what you want.
eg
<?php
somesetupcode();
ob_start(); ?>
<html>
<body>
html text
</body>
</html>
<?php
//This will assign everything that has been output since call to ob_start to your variable.
$myHTML = ob_get_contents() ;
ob_end_flush();
?>
Hope this helps you can read up on output buffers in php docs.
I always recommend to AVOID buffer functions (like ob_start ,or etc) whenever you have an alternative (because sometimes they might conflict with parts in same system).
I use:
function Show_My_Html()
{ ?>
<html>
<head></head>
<body>
...
</body>
</html>
<?php
}
...
//then you can output anywhere
Show_My_Html();
$html_content = '
<p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;
Its REALLY CRAZY but be aware that if you do it :
<?php echo ""; ?>
You will get it:
<html><head></head><body></body></html>
Keep calm, its only php trying turn you crazy.
Related
Consider the following PHP file:
index.php
<!DOCTYPE html>
<head>
<title>Welcome to my home page!</title>
</head>
<body>
<?php
for($i = 0; $i < 2000; ++$i)
{
echo $i.'<br>';
}
?>
</body>
</html>
Is PHP loading into memory only the contents of the echo or the static HTML as well?
Maybe the answer is widely known but I have not found any documentation.
I would love an RTM link btw.
The answer is quite simple, to process/find those <? tags the php interpreter needs to read the whole file.
So yes php loads the whole file.
The ob (output buffer) functionality of PHP actually allows you to take advantage of this.
See the first example of the on_start() documentation illustrates this
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
http://php.net/manual/en/function.ob-start.php
PHP's output buffer contains everything.
See: http://php.net/manual/en/language.basic-syntax.phpmode.php
This works as expected, because when the PHP interpreter hits the ?>
closing tags, it simply starts outputting whatever it finds (except
for an immediately following newline - see instruction separation)
until it hits another opening tag unless in the middle of a
conditional statement in which case the interpreter will determine the
outcome of the conditional before making a decision of what to skip
over. See the next example.
So anything outside of a PHP tag is essentially treated as an echo.
I want to create a lot more cleaner html document/template, which uses the echo command to call the pieces of html/scripts.
Though.. I can't find out how to get it working.
The document I want to reach is like
<html>
<head>
</head>
<body>
<?php
echo 'header.html';
echo 'container.html';
echo 'footer.html';
?>
So this way I can edit each part of the website in an individual document, instead of all pieces of the site in one whole.
<?php
$myString = 'include(portfolio.php); ';
echo $myString;
echo file_get_html('portfolio.php');
echo file_get_html('portfolio.html');
?>
(For trying, i've put a piece of HTML only coding in both a html and php document ( ?>) just for trying if that is any difference..)
But.. You all probably know: This ain't working.
So as simple as your answer could be:
How can I 'echo' a piece/everything within an external file, as in above: echo the 'portfolio.html' content.
Thanks!
p.s. I think echo is my best bet for 'pasting' external coding, but if you got a better, easier, more SEO solution: Be my guest!
Use includes
<html>
<head>
</head>
<body>
<?php
include 'header.html';
include 'container.html';
include 'footer.html';
?>
</body>
</html>
http://php.net/manual/en/function.include.php
Can't echo a file.
use
require_once(dirname(__FILE__).'/header.html');
require_once(dirname(__FILE__).'/container.html');
require_once(dirname(__FILE__).'/footer.html');
I want to put some procedural stuff like html in a variable;
Normally we do this:
<?php
$my_var = '
<h1>some header</h1>
some text.
';
?>
Now I want it to look like this:
<?php
$my_var = '
?>
<h1>some header</h1>
some text.
<?php
';
?>
So that we open a php code, declare a variable, do some stuff outside php, and then reopen php code and close the var and we get that stuff that is outside php, we get it inside that variable.
Thanks.
<?php
ob_start();
?>
<h1>some header</h1>
some text.
<?php
$my_var = ob_get_contents();
ob_end_clean();
echo my_var;
?>
i don't think there is a way because php ignores lines outside the php tags.
if u have the php in a other file user file_get_contents
Please check php heredoc
http://php.net/manual/en/language.types.string.php
Hope it helps
For example…
This PHP code
<?php
echo '<html>';
echo '<body>';
echo '<h1>Header One</h1>';
echo '<p>Hello World!</p>';
echo '</body>';
echo '</html>';
?>
Generates this HTML markup
<html><body><h1>Header One</h1><p>Hello World!</p></body></html>
Are there any functions, libraries or configuration options to make PHP automatically apply some simple formatting (line breaks & indentation) based on the nesting of html tags generated in the output? So that instead something like this would be generated…
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
You could try templating engines like Smarty, Savant, PHP Sugar or VLib.
Or you could go commando with output handling (which I think is a hack). ob_start('ob_tidyhandler');
For the output handling, Tidy might not be installed or enabled, typically the package you will need to install is named php-tidy and you will need to uncomment extension=tidy in your php.ini
You can put html in your PHP script without having to echo it. You also might want to look for a PHP template engine like smarty, so you can separate the view from logic.
I prefer to use heredoc strings and format/indent the HTML myself. Mixing HTML strings inside PHP code quickly leads to unreadable, unmaintainable code. Some advantages of this method:
Quotes don't need to be escaped.
You can put variables inside the heredoc strings.
Even when working with code that loops, you can output the HTML inside heredoc strings. If these strings are indented properly relative to your other blocks of HTML, you will still get HTML code that has good indentation.
It forces you to think about when you want to print HTML, and to print it in blocks instead of lots of little pieces sprinkled throughout your code (very hard to read and maintain).
It's better to separate the PHP code from the HTML as much as you can, whether this means using a templating engine or just putting all of the code before all of the HTML. However, there are still times when it's easiest to mix PHP and HTML.
Here's an example:
<?php
$text = 'Hello World!';
echo <<<HTML
<html>
<body>
<h1>Header One, with some '"quotes"'</h1>
<p>$text</p>
</body>
</html>
HTML;
?>
If I understand your question right, you want to pretty-print the HTML output.
This can be done by post-processing the output of your PHP script. Either by using PHP's output handling feature combined with the tidy extensionDocs:
ob_start('ob_tidyhandler');
Tidy is an extension specialized on cleaning up HTML code, changing indentation etc.. But it's not the only way.
Another alternative is to delegate the post-processing task to the webserver, e.g. output filters in Apache HTTP ServerDocs.
You could do this (my preference):
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
<?php echo '<p>Hello Hello!</p>'; ?>
</body>
</html>
Or:
<?php
$html = '<html><body><h1>Header One</h1><p>Hello World!</p></body></html>';
$tidy = new tidy();
$tidy->parseString($html, array('indent'=> true,'output-xhtml'=> true), 'utf8');
$tidy->cleanRepair();
echo $tidy;
?>";
...would print:
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
...Or you can use the "<<<" operator where you can set formation by yourself:
<?php
echo <<<DATA
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
DATA;
If the below code looks useful to generate html with php, try this library
https://github.com/raftalks/Form
Html::make('div', function($html))
{
$html->table(function($table)
{
$table->thead(function($table)
{
$table->tr(function($tr)
{
$tr->th('item');
$tr->th('category');
});
});
$table->tr(function($tr)
{
$tr->td()->ng_bind('item.name','ng-bind');
$tr->td()->ng_bind('item.category','ng-bind');
$tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
});
$table->setClass('table');
});
$html->setClass('tableContainer');
});
Hard to explain, how to assign HTML contents to PHP variables. The HTML contents are not within the PHP script.as
<?php $a_div = ?><div>Contents goes <b>here</b></div>
Try with ob_get_contents: ob_get_contents() or ob_get_clean()
<?php ob_start(); ?>
<div>Contents goes <b>here</b></div>
<?php $contents = ob_get_contents(); ?>
If its raw HTML, with no PHP in it, stored in separate file it would be just
$a_div = file_get_contents('email.html');
If it's PHP file with HTML mixed with PHP then include it using output buffering like in Robik's answer.
ob_start();
include 'mail.template.php';
$a_div = ob_get_contents();
But if it's relatively small, I'd use HEREDOC
$a_div = <<<HERE
<div>Contents goes <b>here</b></div>
HERE;
If needed (normally I'm using html templates, replacing placeholders by php) ...
there are two ways:
$testVar = <<<EndOfHtml
<div style="border: 48px solid red">test</div>
EndOfHtml;
print $testVar
the easier way, not too elegant - wrap html code in single quotes. so you don't have to escape any double quote:
$test_var = '<div style="border: 48px solid red">test</div>'
helpful? :-)
Are you looking for token-terminated Block-Strings?
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
Best I can do. Unfortunately your question is a little vague.
It's a bit late, and it's not exactly what was asked, but if you're looking to render the page out of order (such as with templating), one could also wrap that section with a function and call it to render it in the desired location.
<?php function RenderFooter() { ?>
<footer>some footer</footer>
<?php } ?>
<h1>Some header</h1>
<main>Some content</main>
<?= RenderFooter(); ?>
you can use DOMDocument To parse html file and get contents you need.