I just wonder how does html comment tag work with php code.
If I have the following code, does php parse it anyway?
<!--
<?php
echo "hi";
?>
-->
a simple question, but I guess it is important to know.
Oh yes, anything in PHP blocks gets executed, but in this case, it isn't shown to the end user because it's in HTML comments.
In the source of the page that is generated by this PHP script, you will see the output surrounded by HTML comments, simple as that.
The only way comments will affect the output of a PHP script is with valid PHP comments.
Yes it does. If that is between <?php ?> tags
You can use PHP comments /* commment */ and they won't execute.
As a side note to the answers above: You can also interrupt the HTML comment:
<!--
<?php
echo "-->This will be seen!<!--";
?>
-->
gives this output:
<!--
-->This will be seen!<!---->
Related
I am inserting a php short code for widget in wordpress page in text mode. But when i go to visual mode and then again to text mode then my short code converts into comments.
Eg: This is my short code :
<?php dynamic_sidebar('footer-sidebar-4'); ?>
and it converts into:
< !--?php dynamic_sidebar('footer-sidebar-4'); ?-- >
after this my short code does not work.
Please get me through out this problem.
<!-- HTML Comments Go Here --> in HTML. PHP executes before it's HTML on the Client. Your sidebar code should be within PHP tags like: <?php dynamic_sidebar('footer-sidebar-4'); ?>. Notice that your code is not PHP at all, since you don't have the tags right. You just have a useless HTML comment.
Get rid of that comment altogether.
<ul id='sidebar'>
<?php dynamic_sidebar('footer-sidebar-4'); ?>
</ul>
The documentation was very clear: http://codex.wordpress.org/Function_Reference/dynamic_sidebar
i am a bit confused when i use conditional statements like
<?php
if(isset($_POST['somevalue'])){
?>
<h1> this is out side php mode now </h1>
<?php
}else {
?>
<h1> again its out php mode </h1>
<?php
}
?>
but yet it still works i mean if $_POST['somevalue'] is set then it outputs "this is out side php mode now" if not it outputs "again its out php mode" my question is if i am outside php mode how does it work then?
I think your question is "how PHP works".as we know php is a server side language.it executes in the server but the scope of the html code will be inside the if loop.so
<?php
if(isset($_POST['somevalue'])){
?>
will be evaluated in server and not in the html part and that will be either true or false.
so after the execution in server your code in front end i.e. in the html part will be like this
<?php
if(1){
?>
<h1> this is out side php mode now </h1>
//as above code is markup language so it will be interpreted by the browser
<?php
}else {
?>
<h1> again its out php mode </h1>
<?php
}
?>
NOTE: the delimiters is for the server to know that the code inside the tag is php code and it will execute it accordingly.
Although you have closed off the executable section of the PHP code, the surrounding if statement and the curly braces will actually have a higher priority as to what is executed and what isn't.
<?php
if
{
// This is considered inside the statement
// and will only be sent if the execution
// makes it inside the statement.
?>
...
<?php
}
else
{
}
?>
// Anything here is simply sent to the browser
// as it will always executed.
<?php
// more code etc
?>
Anything inside the IF statement is considered part of the IF - even if it contains close/open PHP tags.
Basically the PHP control structure overrides open/close tags. This means any sort of if, switch, function etc etc has a higher priority than open close tags.
That's one thing I love about php.
Initially, the main reason is as #Fluffeh mentioned that you are still within the "if" statement.
One way I could put it is that, PHP allows to be embeded in side HTML code. As long as the file has a .php extention then (someone correct me if I'm wrong) Apache knows to use the PHP processor to process that file. It will process the php coding and display the HTML sections in it as well.
Your question is some what similar to
<?php
$name = "Tom";
?>
<h1>Hello <?php echo $name;?>!</h1>
The result will come out as Hello Tom!
it is the same idea as it is inside the php.. satisfy each of the condition and it will run its corresponding statement..
this is one of the best features of php, which allow us to use native html codes rather than putting it inside echo.
A PHP file is processed as plain text/html until it reaches a <?php tag when it executes the php code. When it reaches the closing ?> it processes it as plain text again.
This is exactly the same, but with echo and quote marks around the html you want to print. If you're having trouble reading the code I suggest indenting as needed.
<?php
if(isset($_POST['somevalue'])){
echo "<h1> this is out side php mode now </h1>";
}else {
echo "<h1> again its out php mode </h1>";
}
?>
The way you posted it.
<?php
if(isset($_POST['somevalue'])){
?><h1> this is out side php mode now </h1><?php
}else{
?><h1> again its out php mode </h1><?php
}
?>
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>";
I have PHP code which creates an HTML
echo('<div id="keyboard">');
In my CSS I have
#keyboard {
}
PHPStorm reports the CSS selector is not used. It is used. Can I make it realize that? In not, I once saw some way of disabling a single error or warning, but I can no longer find that in the documentation.
What you can try is to move the html out of the php script and just open the parentheses when needed. I suspect that the IDE cannot distiguish between php echoed html and html outside of php tags.
<?php
//add php code here
?>
<div id="keyboard">
<?php
//php code here
?>
</div>
<?php
//add php code here
?>
The IDE should now be able to match the CSS with the relavant id.
Is there a way to hide commenting in my php/html file?
I want to add markup that I don't want people to be able to view in source in their browsers.
Is this possible?
<!-- Prevent this comment from being viewed -->
<?php...
Thanks in advance.
If you add comments as PHP, people won't see it in their browser.
<div>
<!-- This HTML comment can be seen by people -->
<?php //But this PHP comment can only be seen by me :) ?>
</div>
http://en.wikipedia.org/wiki/PHP
I see what you mean. You can do that with output buffering:
<?php
// this is not
?><!-- this is sent to browser -->
And with output buffering.
<?php
ob_start();
?><!-- this is NOT sent to browser --> <b>This isn't sent as well!</b> <?php
ob_end_clean();
?>
However, if you want to remove comments only, you need to do some parsing:
<?php
ob_start();
?><!-- this is NOT sent to browser --><?php
$html=ob_get_clean();
// TODO: Use a DOM parser to parse $html and remove comments from it.
?>
That does sound a bit over-engineered though...
Just swap lines above and use php comment:
<?php...
// Prevented this comment from being viewed