I am generating a large amount of HTML code with PHP using echo and it is all appearing on one line.
I understand that HTML ignores whitespace, and there are thousands of answers which point out that you need <BR> or a block element. But that is not my question.
My problem is that is very hard to debug my HTML source code when it is all on one line. I am using Windows 7 and the Firefox browser tool Page Source to show the source code.
For a simple example, if the text editor I use shows
<HTML><BODY>
Hello, World!
</BODY></HTML>
Then the browser source code tool shows exactly that too, and when I generate it with PHP like
<?php
echo "<HTML><BODY>";
echo "Hello, World!";
echo "</BODY></HTML>";
?>
then the browser tool shows, as you would expect,
<HTML><BODY>Hello, World!</BODY></HTML>
I want to break the lines, and have tried
<?php
echo "<HTML><BODY>\n";
echo "Hello, World!\n";
echo "</BODY></HTML>\n";
?>
and with "\r\n" and with "\xA" and also like this
<?php
echo "<HTML><BODY>" . PHP_EOL;
echo "Hello, World!" . PHP_EOL;
echo "</BODY></HTML>" . PHP_EOL;
?>
yet the content stays resolutely on a single line.
This works on windows
<?php
echo "<HTML><BODY>
";
echo "Hello, World!
";
echo "</BODY></HTML>
";
?>
Related
The <br> and <hr> tags don't work in PhpStorm and when I'm running my index.php file in PhpStorm whit this code :
<?php
$a=10;
echo "This is $a";
echo "\n";
echo 'This is $a';
echo "\n";
echo "<hr>";
echo "<br>";
print "This is $a";
?>
I'm getting this in console:
This is 10
This is $a
<hr><br>This is 10
Why is this happening?
The PHPStorm console is show you your exact output. And It's accurate.
The <hr> and <br> tags are HTML. In the console you'll see them just like this.
When you load your page in a web browser those HTML tags will display as a horizontal rule and a blank line, as you expect.
See here for more info about the different options PHPStorm provides for running your PHP code:
https://www.jetbrains.com/phpstorm/help/running-php-applications.html
How can I use the HTML <code> element to output a block of PHP code, without the page running that PHP code? Eg;
<pre><code>
<?php
// Some super duper PHP code
?>
</code></pre>
I'm creating an API docs page, which features snippets of PHP that anyone wishing to use the API can use as examples, but anything wrapped in <?php> tags runs as an actual PHP function
Use <?php and ?>.
The HTML entities will show up as PHP opening and closing tags when the page is rendered, but PHP will obviously not see them. But you have to html-escape your code anyways, otherwise contained HTML-tags will be rendered. So there should be
<?php echo 'Hello, World.<br>'; ?>
Another way would be to have a string specified by a nowdoc and then output html-escaped (demo):
<?php
$code = <<<'EOC'
<?php
echo 'Hello, World.<br>';
// ...your code here...
?>
EOC;
echo htmlentities($code);
?>
Have look for different approaches at How do I display PHP code in HTML?.
Do this via PHP like so:
<?php
$code = '<?php
echo "Hello, World!";
?>';
echo '<code>' . htmlspecialchars($code) . '</code>';
?>
try something like this:
<?php echo '<?php'; ?>
This may help you.........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
I had to convert the less-than and greater-than to their HTML name.
<pre><code><?php echo
"<!--
This is the church title to be used in the heading of the web-pages.
Author: John Fischer III of Written For Christ in 2018
Updated:
-->
<?php echo 'Our Little Church:'; ?>" ?>
</code></pre>
You all might say this is a foolish question submitted here but can anyone give any valid reason for the output I am getting. This is amazing and strange because "\n" forgot its work. I am getting this output
Test Test Test Test Test Test Test Test
for this code:
<?php
for($i=1;$i<5;$i++)
{
echo "TEST \n";
echo "TEST"."\n";
}
?>
You're looking at it in HTML; in HTML, new lines are treated like regular spaces. Look at the source for the page and you will see the new lines there.
If you want output in new line try this...
<?php
for($i=1;$i<5;$i++)
{
echo "TEST </br>";
echo "TEST </br>";
}
?>
I have got a string contain with html tags.I want to print it using php , but it print with html tags.How to print somethings like this?
String :
<p style="color:#f00">HTML basic test Text</p>
Want to show :
HTML basic test Text
currently I show the result : <p style="color:#f00">HTML basic test Text</p>
Have you any solution?
Like CBroe said, you can use strip_tags() function
<?php
$text = '<p style="color:#f00">HTML basic test Text</p>';
echo strip_tags($text);
?>
AFAICT an echo should do it. If I need some HTML code to be generatet with php I echo it like:
<?php
echo '<p>Hallo world</p>';
?>
Worked for me, I never used print for that purpose.
I am using PHP 5.3.4 with Apache 2.2.17 on a Windows 7 x64 system. I would like to have my PHP page output the result of a system call in real-time to the user's browser. To that end, I have configured output_buffering=Off in php.ini and created this code:
<?php
ob_implicit_flush(true);
ob_end_flush();
system('ping -n 10 www.google.com');
?>
The result of the ping is printed in real-time, but I also get a PHP diagnostic error and callstack at the top of my page that says:
Notice: ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush in index.php on line 3
What do I need to do to either correct or suppress this error?
Update
If I change ob_end_flush() to $a = 1/0; I get a similar error and the output is realtime in all browsers. Is it something about the way the exception is printed that causes this to work?
some web browsers buffer the first x bytes before they start to render a page, under certain conditions.
try just outputting lots of whitespace first
I have a solution that works, but it is non-performant and icky. I throw an exception, but hide the exception dialog.
<?php
ob_implicit_flush(true);
// Something about the way exceptions are thrown causes Firefox and Chrome
// to be able to display the results of the system call in real-time rather
// than having to wait for the call to complete. So, I just hide the
// exception message. IE9 works with or without this.
echo "<div style=\"display:none\">";
$a = 1/0;
echo "</div>";
echo "<pre>";
system('ping -n 5 www.google.com');
echo "</pre>";
?>
To auto-scroll to the bottom of the page, I add some javascript:
<html><head>
<script language="javascript">
var int = self.setInterval("window.scrollBy(0,1000);", 200);
</script>
</head>
<body>
<?php
// insert above php code here
// stop scrolling when the execution finishes
echo '<script language="javascript">int = window.clearInterval(int);</script>';
?>
</body>
</html>
EDIT
#Chris's answer shows a much better solution.
echo '<div style="display:none">';
for ($a = 0; $a < 768; $a++)
echo ' ';
echo '</div>';
Just add this to flush the buffer:
if (eregi("chrome",$_SERVER['HTTP_USER_AGENT'])) {
echo "<div style=\"display:none\">";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo " ";
echo "</div>";
}
It has to be inside the loop. or between two output you need to show in real-time.
AND here's the shorthand trick:
add this:
if (eregi("chrome",$_SERVER['HTTP_USER_AGENT'])) {
echo "<div style=\"display:none\"></div>";
}
ob_end_flush() flushes the php output buffer, and requires an active output buffer created with ob_start().
I think you just want to call flush() to send the data to the client.
<?php
ob_implicit_flush(true);
flush();
system('ping -n 10 www.google.com');
?>
In your code, the error is pretty easy to interpret.
You call ob_end_flush() on line 3, but (like the error says), there is no output to flush. In essence, line 3 is useless because no output has been sent, so deleting the line will fix the error. If this is incorporated into a larger file, you might need to keep ob_end_flush() because some output may already have been captured.
EDIT: Since you need to flush it, either:
a: add ob_start(); to the top of the file.
b: replace ob_end_flush(); with flush();
EDIT2: Since the first didn't seem to work, this is the best I can offer: How to echo output in real time, (before script finishes)?