Reputation: 21
I have two different interpretations on two different hostings of the following php code:
<?php /*
<div> <?php echo "Hello world!"; ?> </div>
*/ ?>
On my test server this renders nothing - like I would expect because everything between /* */ is commented out.
On the live server on the other hand, the comment stops at the second <?php
. So it renders only the closing div
tag. This obviously can mess up the whole document.
So is the use of comments wrong in this case? Why do I get different results on different servers?
I use php in wordpress theme developpment.
Edit: As I published the post I already got half of the answer out of the code box. Here the comment also stopps at the closing div tag.
But why do I get a different result on my test server. Also in my editor the whole block is marked as commented out.
Upvotes: 2
Views: 565
Reputation: 2968
Actually you are using PHP within the HTML <div> <?php echo "Hello world!"; ?> </div>
and using PHP comment for HTML /*<div> <?php echo "Hello world!"; ?> </div>*/
, but within the comment there is ?>
which is creating problem for you.
So to solve your problem just do:
<?php
$string = "Hello world!";
echo "<div>$string</div>";
?>
And if you want to comment out the <div>
simply do:
<?php
$string = "Hello world!";
// echo "<div>$string</div>";
?>
Now you will get the same result on all servers.
Upvotes: 0
Reputation: 4211
Php standard multi line comment as below Look at phpdoc
/**
* @return null
* @param int $int
*/
function boo($int)
{
}
Upvotes: -3
Reputation: 4148
The first time you put the ?>
you actually closing the main php script frame.
Can't say why your testing server ignore it, but you can't comment out the PHP frame itself, just the code on it...
So anything after ?>
will act as html tags (if it got < >
) or as plain text.
Upvotes: 0
Reputation: 80
PHP executes between it's opening and closing tags,
<?php /*
<div> <?php echo "Hello world!"; ?> </div>
*/ ?>
Notice, the closing tag before </div>
, PHP stops executing after that and so, it's no use of the 3rd line */ ?>
here, it's plain text displayed in the html.
The <?php
tag after opening <div>
is already inside the comment hence, it is throwing any error.
Try removing the ?>
before closing tag of </div>
or adding <?php
before your ending commented code. You should see the difference. Just Like
<?php /*
<div> <?php echo "Hello world!"; ?> </div>
<?php */ ?>
Upvotes: 2