开发者

How do I echo one HTML line in PHP?

I want to echo this line in PHP simp开发者_运维问答ly by using "echo", but it doesn't work. How can I fix it?

echo '<p><input type="button" name="Back" value="Back" onclick="window.location ='viewusers.php'" /></p>'


You need to escape the quotes, or better, change to single quotes like this: (still needs escaping at the end there)

echo '<p><input type="button" name="Back" value="Back" onclick="window.location =\'viewusers.php\'" /></p>';

It's better to do single quotes because then php doesn't parse it like it does with double quotes. A better explanation is in the php docs.

Another option is to close the php block, like this: (then you don't have to worry about escaping quotes)

<?php
  [your php code]
?>
[your html line or block]
<?php
  [more php]
?>


echo '<p><input type="button" name="Back" value="Back" onclick="window.location =\'viewusers.php\'" /></p>'

Notice the simple quote


It's because you're using " inside what you want to echo. All you need is escape quotes.

This should do it:

echo '<p><input type="button" name="Back" value="Back" onclick="window.location =\'viewusers.php\'" /></p>';

Another option is to do it in blocks. This would be better if your doing a lot of echos like that. Such as:

<?php
if($output == "hello"): 
?>

<p><input type="button" name="Back" value="Back" onclick="window.location ='viewusers.php'" /></p>

<php 
else:
?>
<p>Doesn't equal hello</p>

<?php
endif;
?>

Hope it helps!


You need to escape your double quotes like this (because you use them to delimit your string):

echo "<p><input type=\"button\" name=\"Back\" value=\"Back\" onclick=\"window.location ='viewusers.php'\" /></p>";

OR (As per your edit with single quotes)

echo '<p><input type="button" name="Back" value="Back" onclick="window.location =\'viewusers.php\'" /></p>';


Basically you need to escape the " which are found in a string enclosed in ".

Alternatively to avoid escaping you can use here docs as:

echo <<<FOO
<p><input type="button" name="Back" value="Back" onclick="window.location ='viewusers.php'" /></p>
FOO;


Like everyone has said, you need to escape the quotes within the string you're echoing. echo "

";

Or, better yet, since this line itself isn't actually dynamic, end the php block and just leave it as HTML:

?>
<p><input type="button" name="Back" value="Back" onclick="window.location ='viewusers.php'" /></p>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜