echo字体颜色输出

I wrote a php code that display error in red color. But somehow, this doesn't seem to work out. Here's my code:

<?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>

Do this something like that --

echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';

Your "" quotes are conflicting

You are generating invalid HTML. A <span> element cannot contain a <center> element. Browsers may error correct by moving the <center> element so it is outside the <span> (so the style rules applied to the span won't inheit into the <center> element).

Generate valid markup (and move your style rules to a stylesheet).

echo "<p class='error'>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</p>";

Your quotes are conflicting. Either escape them, or use single and double quotes

 echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';
    <?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style='color: red;' /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>

try this you were closing the quotations

Modify with single quotes in color attr and remove 'center' also, echo "<span style='color: red;' />

The problem is un-escaped quotes on your PHP expression.

echo "<span style="color: red;" />...
                //^ Right here        

Because, Your PHP echo statement also started with the same quote i.e ".

Here are the different ways you can solve this:

  1. Use mixed quotes

    echo "<span style='color: red;' />...
          // Single quote in the HTML part
    
  2. Escape the quotes

    echo "<span style=\"color: red;\" />...
         // Escape the quotes so that it gets treated as string rather than a modifier