Commenting php tags - php

I got this code below from a tutorial I'm using to learn PHP. I know that // is used to comment out code. In the first line of the code below, you see {// subject selected ?>
Is the php tag ?> not commented out by the // along with the subject selected text?
<?php if (!is_null($sel_subject)) {// subject selected ?>
<h2><?php echo $sel_subject['menu_name'];?></h2>
<?php } elseif (!is_null($sel_page)) {// page selected ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or page to edit</h2>
<?php } ?>

the BEST place to address such questions is an official man page:
The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.
I can assure you that it's way more reliable source of knowledge than some volunteered help from some enthusiast

No, ?> is not commented out.

no ?> are not commented out with the in line comment, where as the block comment they are.
http://codepad.org/YUhG2DTd
Example: The following ?> does not get commented out.
<?php
\\?>
echo 'works';
?>
where as the following does get commented out.
<?php
/*
?>
*/
echo 'failed';
?>

{// subject selected ?>
No, the ?> is not commented. Because, that is not part of a php statement. That is a tag that apache uses to determine. Apache will send the contents enclosed by the tags to php and place the output from php in its output buffer.

Related

Insert data in HTML from php

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

Add Google Plus in header in html.tpl.php

In html.tpl.php I have written :
<?php if($front_page):?>
<?php print 'Google+ '?>
<?php endif; ?>
but its not working moreover its hanging the home page.
Can someone please tell me the correct syntax if it has some error
Should you not be using echo instead of print?
<?php if(drupal_is_front_page()):
echo 'Google+';
endif; ?>
In most themes, you should be overriding page.tpl.php rather than html.tpl.php. You also have a couple of errors in your syntax. The typical code for a template would read:
<?php if($front_page): ?>
Google+
<?php endif; ?>
Note the space in the first line, plus you don't need to print the second.

php syntax for big block of echo <?php if(1): ?> html<?php endif; ?> correct way

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.

Reasoning for alternate PHP syntax for conditional HTML

So, why does this work:
<?php if (condition): ?>
<!--html code here-->
<?php endif; ?>
But not simply this:
<?php
if (condition) { ?>
<!--html code here-->
<?
}
I figured it was just style preference, but I actually can't seem to get it to work the second way. Am I just doing something completely wrong? I can't see the purpose of outputting HTML right in the middle of an if statement if you wanted it to always print.
You need to allow short tag in your php.ini to make <? work
else you have to write <?php } ?>
The second way should work like you described considering you have the closing php tags and < ?php
It does, if you have the right re-opening tag...
<?php
if (condition) { ?>
<!-- html code -->
<?php
}

How do HTML comments work with PHP code?

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!<!---->

Categories