How do I print php code in a print / echo ?
What I mean is this:
<?php echo "<?php ?>"; ?>
Should output: <?php ?> on my screen, but I receive a blank page. Any special escaping that I have to use ?
<?php echo htmlspecialchars('<?php ?>'); ?>
Akam's solution sorts out the PHP, if the Content-Type of the returned file is HTML.
Alternately, you could change the Content-Type to Text, thereby bypassing the HTML rendering.
<?php
header( 'Content-type: text/plain' );
echo '<?php ?>';
?>
Of course, this would affect the whole page, and not just a segment of it. As such, it would be useful it you were displaying the contents of a PHP script file as a standalone page, but if you were wanting to show snippets of PHP code within an HTML page, then Akam's solution would be better suited for that.
If you print and you pretend to see it as HTML, the browser will interprete the tag and show nothing, but you will still be able to see it if you look at the source code.
To show the < and > tags properly you should use < and > or use the htmlentities() or htmlspecialchars() functions:
<?php
echo htmlentities( '<?php ?>' );
?>
There is another solution built on str_replace which accepts arrays for its parameters search and replace.
<?php
echo str_replace(array('<','>'),array('<','>'),'<?php ?>');
?>
Check out the following demo: http://phpfiddle.org/main/code/3hp-itx
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 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('...');
}
I have created an index.php page in MAMP.
My index.php reads exactly like the following. I access it through localhost:8888.
<?php
echo file_get_contents("http://stackoverflow.com");
?>
However, instead of returning the html source code from this page as I believe it would do, it just returns http://stackoverflow.com as a regular webpage, like the webpage you are looking at now.
My MAMP is using PHP 5.5.10. The user_agent is set and allow_url_fopen is on.
I am severely confused. I would very much appreciate any explanations :)
It IS returning the html and the browser is interpreting it.
You can try wrap the output in tags:
<?php
echo '<code>' . file_get_contents("http://stackoverflow.com") . '</code>';
?>
Or set headers as text/plain instead of html:
<?php
header('Content-Type: text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
Or if you want to keep the headers and not inject the output into code tags:
<?php
echo htmlspecialchars(file_get_contents("http://stackoverflow.com"));
?>
I prefer the last one.
If you want to see the plain text you can use the following,
<?php
header('Content-Type:text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
What you see in your version is correct, since the HTML is rendered by your internet browser.
The results of a php script are by default sent to the bowser, so your code
<?php
echo file_get_contents("http://stackoverflow.com");
?>
Is reading the web page and then sending it to your browser. So it looks like it is just showing the page you read.
If you change it to
<?php
$page = file_get_contents("http://stackoverflow.com");
?>
Then you can do something with the web page source stored in $page.
How would one go about showing PHP code on user end. Sort of like w3School does?
Having lets say a grey area div, and then showing the code in there without activating it?
You can use html entities <?php in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
Use <pre> or <code> tags to wrap your code.
Take a look at http://php.net/manual/en/function.highlight-string.php to further see how you can make the code look pretty.
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
Something like:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
Simply you can use following code to display php code on webpage.
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
<?php print('This is php code.'); ?>
The first step is to not wrap that code in PHP tags. So instead of this:
<?
var sample = "code";
?>
You would have this:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre tags to get rid of HTML-ish formatting:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre tags (or any other tags you want to use for displaying code, such as div) as you see fit.
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettify often comes highly recommended.
Typically this is done by showing code within <pre> or <code> tags.
You can use this template........
######################################################################
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>";
#######################################################################
It will show the source code and output following.
use the header function of php, this will rea
<?php
header("content-type: text/plain");
?>
The PHP code will just be a string that you can echo or print onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.
Ex:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";
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.