June 14, 2012

echo font color output

Question by xan

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{...


?>

Answer by swapnesh

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

Answer by Starx

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
    

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!