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>";
Related
I have some large HTML code that I want to put in a PHP variable without losing the syntax highlight. I would also like for when it is put in as a variable, it will be shown as a normal string not HTML code.
Is there a way like:
<?php
$var =
?>
HTML GOES HERE
<?php
;
echo $var;
?>
I know my code here doesn't make sense, but it's just to describe the situation.
If the code is very long - say a menu div or something, you can use include. It won't give you a variable to use, but the result is the same.
<?php
include file_with_my_code.php;
?>
In file_with_my_code.php (doesn't have to be .php, .html works just fine as well) you simply paste the code as if you were creating a standalone html file.
Just found it!
$var = <<<HTML
<HTML GOES HERE>
HTML;
I was wondering if is it possible to change an already written variable in the html generated file. Maybe there is an option to 'publish/write' the html file in the very end of php processing.
<html>
<?php
echo '<h1>' . $pageTitle . '</h1>';
?>
[...]
<?php
[DB queries]
$pageTitle = "New Page title";
echo "<javascript-code-to-change-the-page-title>";
?>
Yes I could set $pageTitle before, but it may change along the code according to some query.
So, I figure out that I can change the page title on client side only.
I'm probably missing some logic here.
You could use a combination of (A) PHP's Output buffering, and (B) PHP's DomDocument class.
Basically, you would capture the HTML output by wrapping the output in ob_* commands. Once you get the output, you throw it in the DOM parser. Once you're there, you can then traverse the DOM document and make your changes. Afterwards, you can dump everything back out to the browser.
I most definitely would not rely on JS to do any changes on the page.
ALTERNATIVELY, you could do your PHP at the top of the file, then echo the variables out as needed.
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 node with the following content (input mode: php code):
this is a <?php echo "test" ?>
If I output node->body, the output is
this is a <?php echo "test" ?>
What I want is:
this is a test
What is the easiest way to do this?
(I don't want all the default divs and other structural stuff coming with it when I call node_view)
You should enable (shipped with core) "php.module", which comes with an input-formatter, PHP-filter. That filter allows you to insert php as you mention in nodes. And will eval() that for you on output.
It sounds like you don't use the php filter that's needed to do something like this. The result is your code is escaped.
On a random drupal install I just tested:
This is a <?php echo 'test'; ?>
Outputs
This is a test
When I view the node.
In Drupal, to display html output of a node which have 'PHP Filter' you can do this
echo php_eval(node->body);
You could output
eval($node->body); // be sure what you are evaling
Is it possible for PHP file to print itself, for example <?php some code; ?> that I get output in HTML as <?php some code; ?>(I know its possible in c++), if not is it possible to actually print html version of php code with nice formatting and colors such as from this url inside code container http://woork.blogspot.com/2009/07/twitter-api-how-to-create-stream-of.html. OR from this website when you press code, while posting your example your code gets wrapped or whatever term is for that, makes it distinguishable from other non-code text. tnx
Yes.
<?php readfile(__FILE__)
__FILE__ is a magic constant that contains the absolute filesystem path to the file it is used in. And readfile just reads and prints the contents. And if you want to have a syntax highlighted HTML output, try the highlight_file function or highlight_string function instead.
I'm not sure if this is exactly what you want but you can print a file using:
echo file_get_contents(__FILE__);
or syntax-highlighted:
highlight_file(__FILE__);