How to output formatted HTML from PHP? - php

I like to format all my HTML with tabs for neatness and readability. Recently I started using PHP and now I have a lot of HTML output that comes from in between PHP tags. Those output lines all line up one the left side of the screen. I have to use /n to make a line go to the next. Is there anything like that for forcing tabs, or any way to have neat HTML output coming from PHP?

If there is relative bigger blocks of html you are outputting then HEREDOC syntax would help you format the html the way you want witout bothering much about echo tabs using php.
$html = <<<HTML
<html>
<head><title>...</title></head>
<body>
<div>$phpVariable</div>
</body>
</html>
HTML;
If you use some tool to parse your html , remember it will also add an extra overhead of processing and data payload for each request so you might want to do it only for debug purposes.

There's the tidy extension which helps you to (re-)format your html output.
But it has a little price tag attached to it. Parsing the output and building an html dom isn't exactly cost free.
edit: Could also be that you're simply looking for the \t "character". E.g.
<html>
<head><title>...</title></head>
<body>
<?php
for($i=0; $i<10; $i++) {
echo "\t\t<div>$i;</div>\n";
}
?>
</body>
</html>
or you nest and indent your php/html code in a way that the output is indented nicely. (Sorry for the ugly example:)
<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++) { ?>
<div><?php echo $i; ?></div>
<?php } ?>
</body>
</html>

<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++) { ?>
<div><?php echo $i; ?></div>
<?php } ?>
</body>
</html>
Actually this is a good example but in this case it's better to use Alternative way of doing things
<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++): ?> // notice the colon
<div><?php echo $i; ?></div>
<?php endfor; ?>
</body>
</html>
Reference

Related

How can I use 20 lines of HTML code in PHP without breaking up the PHP tags (<?php ?>)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wanted to create a form and some other HTML inside some PHP code but without breaking up the tags.
Example:
function paypal() {
// HTML code
}
So is there a way I can use a block of HTML code in PHP?
You can use heredoc syntax.
For example, as an alternative to this:
if ($something) { ?>
<div>
</div>
<?php }
You can do this:
if ($something) {
echo <<<HTML
<div>
</div>
HTML;
}
Interestingly enough, I saw someone doing just that earlier today. Here's an example solution using heredoc syntax:
echo <<<ENDFLAG
... some text/html here ...
... more lines of HTML ...
ENDFLAG;
ENDFLAG can be anything. Here's more info: http://www.tuxradar.com/practicalphp/2/6/3
I'm guessing what you are trying to is change something like this:
<?php //some php code here ?>
<p>Some html code here</p>
<br>
<p>Some html code here</p>
<?php //some other php code here ?>
etc...
You can just do
<?php
//some php code here
echo '<p>Some html code here</p>';
echo '<br>';
echo '<p>Some html code here</p>';
//some other php code here
?>
Your function can do the same...
<?php
function paypal() {
//Do some fancy php
$i = 'paypal';
$retVal = '<p>' . $i . '</p>';
return $retVal;
};
echo paypal();
?>
You can do it with echo, just put all the lines after an echo and it will work perfectly. example :
<?php echo ' <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>';
?>
<?php
//your php code
echo '<html></html'; //your html code
?>
But it's very bad way to use html code in php with echo.
I recommend you to use html templates (mustache - https://github.com/bobthecow/mustache.php)

Return big HTML block with some php without make string

How can i return big html block with some php by using <<<HTML HTML; .
return <<<HTML
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php } ?>
<?php echo $whatever; ?>
HTML;
I can't understand what will work and what will not! how should i use this kind of return <<<HTML HTML; block with some php variable that i need to echo and some function that echo some thing (not return)
You can use 'capture output' for this task. see Output Control Functions
i has some example code that i have just tested. It captures the output of the div tag in $out1 and shows it again later.
This technique is used in many 'templating' libraries and in 'views' in the 'frameworks'.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test of Output control functions</title>
</head>
<body>
<?php ob_start(); // capture the buffer ?>
<div style="border: 4px solid red">
<p>This is a test paragraph</p>
<p>This is test PHP code: <?php echo time(); ?></p>
</div>
<?php $out1 = ob_get_contents(); // end capture ?>
</body>
</html>
<?php echo $out1; // output now or save for later. ?>
<?php var_dump($out1, strlen($out1)); ?>
<?php exit; ?>
Okay, google heredoc syntax for PHP.
but this is how it works (which I think you are trying to do.
$html = <<<HTML
<div>
<h1>$phpVariableTitle</h1>
<div>
{$thisFunctionEchosomthingNotReturn()}
</div>
</div>
HTML;
return $html;
Try that. IMPORTANT! heredoc syntax requires your closing tag be left aligned with no tabs. So make sure there are no spaces or tabs to the left of your heredoc tags, in this example my heredoc tags are called HTML. Also, wrapping your php variables/functions with curly braces is optional but good practice for this method. NO PHP tags in side heredoc block.
Hope that helps.
To make a conditional statement work inside you need to use a function:
class My_Class {
public function myCondition($param) {
if($param === true) {
return '<p>True</p>';
} else {
return '<p>False</p>';
}
}
}
$object =new My_Class();
$html = <<<HTML
<div>
<h1>Conditional Statement</h1>
<div> {$object->myCondition(true)} </div>
</div>
HTML;
something like that should work. But I haven't tested it.
I am unable to understand your question properly may be this may help:
<HTML>
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn();
if($isflag){?>
<span>DO not do this</span>
<?php }//Closing If if it ends here.
echo $whatever; ?>
</HTML>
You cannot write control structures / functions logic inside of HEREDOC syntax.
Alternate way..
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php echo $whatever; }?>

Include php file in html page

I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");

echo text alongside a generated value PHP

I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>

How do I insert an HTML page using PHP?

I wanted to know, is there any way to insert an HTML page into PHP without using the include function? I do not want to use an external html file, I want to write the html coding directly into the php coding.
Thanks for your help!
Interleave it:
<?php
// Some php code.
?>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>Header</h1>
<?php /* More php code. */ ?>
<p>Blah!</a>
</body>
</html>
<?php /* Even more php. */ ?>
From a best practices point of view, though, avoid doing this - having business logic (PHP) and presentation (HTML) in the same place makes maintaining harder.
EDIT: To address your comment. You can either do it the same way, or use echo:
<?php if (x == 5) { ?>
<p>Blah!</a>
<?php } else {
echo '<p>Bleh</p>';
} ?>
If you need to include snippets of HTML based on conditions, you can interleave code like this. In this case it's convenient to use the alternative syntax for loop controls
<?php if ( $var ): ?>
<html>
<title>YAY</title>
</html>
<?php endif; ?>
so the code is clearer to read and you retain HTML syntax coloring (if your editor supports it).
It is very bad habit to mix HTML and PHP (for more than just output control), but here you go:
$html = "<div>This is HTML</div>"
echo $html;
or Heredoc syntax:
$html = <<<EOF
<div>
<p>
Some longer HTML
</p>
</div>
EOF;
echo $html;
or using alternative syntax for control statements if the output depends on some condition (or if you loop through an array etc.)(which is far better than building HTML with strings):
<?php if($foo): ?>
<div> Some HTML output </div>
<?php else: ?>
<div> Some other HTML </div>
<?php endif; ?>
or just
<?php //PHP here ?>
<div>HTML</div>
<?php //more PHP ?>
<div>more HTML</div>
<?php //even more PHP ?>

Categories