开发者

how to style php echo output

It's probably stupid question, but I can not find an answer. How can I style echo output with css? I have this code:

echo "<div id="errormsg"> Error &开发者_如何学运维lt;/div>";

Now it displays syntax error, I think because of those quotes around errormsg. I've tried single quotes, but with no effect. Thank you


When outputting HTML, it's easier to use single quotes so you can use proper double quotes inside like so:

echo '<div id="errormsg"> Error </div>';

That will get rid of your parse error... To edit the style you will need to use CSS with the selector of #errormsg like so:

#errormsg {
    color: red;
}


try

echo "<div id=\"errormsg\"> Error </div>";


First you need to either use single-quotes to surround the attribute value:

echo "<div id='errormsg'> Error </div>";

Or you could reverse that, to give:

echo '<div id="errormsg"> Error </div>';

Or you should escape the quotes:

echo "<div id=\"errormsg\"> Error </div>";

And then style the resulting element with the CSS:

#errormsg {
    /* css */
}

The syntax problem you were encountering is a result of terminating the string and then having a disparate element between the first and second strings, with which PHP has no idea what to do.


To put double quotes inside of a double-quoted string, you need to "escape" them by putting blackslashes before them:

echo "<div id=\"errormsg\"> Error </div>";

In this case, another choice is to use single quotes for one or the other.

echo "<div id='errormsg'> Error </div>";
echo '<div id="errormsg"> Error </div>';

PHP's documentation has a section explaining the different string syntaxes, which should explain everything you could want to know about this subject.


Use single quotes around errormsg and what you have should work just fine. Alternatively, but less tidy, you can escape the double quotes with a backslash.

echo "<div id='errormsg'> Error </div>";


You are getting a syntax error because you are including unescaped double quotes inside a string that is delimited by double quotes.

Either escape them

echo "<div id=\"errormsg\"> Error </div>";

or use single quotes

echo '<div id="errormsg"> Error </div>';

The browser doesn't care if you generated markup using echo or something else. It just sees the HTML you send to it.

For the above markup, you can style it using an id selector:

#errormsg { /* … */ }

The usual rules for the cascade (including specificity) will apply.


If you don't want to care about single quotes or double quotes then the better way to achieve your answer is to use heredoc syntax .

Your solution :

<?php
$heredoc = <<< EOT
<div id="errormsg">Error solved</div>
EOT;
echo "$heredoc";
?>

css :
#errormsg{color: green;}

WARNING :

  1. Do not add whiteSpace after <<< EOT
  2. Do not add whiteSpace before EOT;
  3. Do not add whiteSpace between EOT and ;
  4. Do not add whiteSpace after EOT;
  5. EOT; must be in new line.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜