开发者

How do I Escape PHP inside of a echo function?

What I have is this:

function add_email_form () {

 echo "<form class=\"email-me-form\" id=\"initialize\" action=\"<?php echo $_SERVER[PHP_SELF] ?>\" method=\"post\" nam开发者_如何学Goe=\"contact_me\">\n";

} 

How do I make this syntactically correct?


Don't use double quotes unless you need to. Use single quotes, '. That way, you don't have to escape anything except control characters like the \n, and in that case, do drop to double quotes. So the above would be:

echo '<form class="email-me-form" id="intialize" action="'.$_SERVER['PHP_SELF'].'"
      method="post" name="contact_me">'."\n";

(newline added to I don't cause a horizontal scrollbar) You don't need to do htmlspecialchars() looks like what you want.

You don't need to (and in fact, cannot) call "<?php echo?>" inside a PHP statement. Only when you're outside of PHP does that work. In this case, just concatenate with ..

And as stated in the comments, you should quote array keys when they're strings, as otherwise PHP will throw a warning and could potentially be confused.


Something like so:

echo "<form class=\"email-me-form\" id=\"initialize\" action=\"",
  htmlspecialchars($_SERVER['PHP_SELF']),
  "\" method=\"post\" name=\"contact_me\">\n";


function add_email_form () {
    echo "<form class=\"email-me-form\" id=\"initialize\" action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\" name=\"contact_me\">\n";
} 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜