Quick Question about changing font colour within PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Quick Question about changing font colour within PHP

Post by 3.14 »

I can change the colour, font, size etc in html, but how do I do it using PHP, or do I have to go between php and html in order to do it?

an example would be nice if you've got one.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

PHP cannot replace HTML - PHP can be used to help build an HTML page but you need to use HTML to present information to the user.

Mac
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

just to add some details:

all your echo (or print) commands should contain is some html stuff that you send as a result to your client's browser.

So to solve your problem use

echo "<span style=\"color:#FF0000\">red</span>";

I use a backslash before the "" in the style attribute to tell php that those " don't mark the end of the echo argument, when working on your code PHP will internetpret it and delete all the php stuff so in the end you will see
<span style="color:#FF0000">red</span>
as the source code

hope I helped you
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post by 3.14 »

Thank you, that helped.

The backslashes were where I was going wrong.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

I hate to keep preaching, but you shouldn't use inline style if you can avoid it, and you definitely should never use HTML style elements (which hasn't been done here, just so there's no confusion).
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You know, you don't have to add thoses backslashes. I use a GUI to program, Dreamweaver MX, and thoses slashes screw with it's display in the preview window so i switch between PHP and regular HTML sometimes in my scripts.

Code: Select all

<?php
// some PHP code
?>
<span style="color:#<?php echo $colorcode; ?>"><?php echo $color; ?></span>
<?php
// more PHP code
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sami wrote:You know, you don't have to add thoses backslashes. ... i switch between PHP and regular HTML sometimes in my scripts.
Just to add to this, you can also use single quotes around the echo:

Code: Select all

echo '<span style="color: #FF0000;">red</span>';
or heredoc format:

Code: Select all

echo <<<END

<span style="color: #FF0000;">red</span>
END;
For more info on strings and PHP:
http://www.php.net/manual/en/language.types.string.php

Mac
Saint
Forum Newbie
Posts: 4
Joined: Wed Sep 10, 2003 10:55 pm
Location: CO, USA
Contact:

Post by Saint »

Sorry to bring up an old post, but there's always a rule against posting something new that has allready been posted. So here's my question, are there any advantges to using any of the formats over the others? Or is it just personal preference?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Normally I prefer people to start new posts rather than add to old ones and reference the old post in the new topic (see the guidelines link in my signature). However, for this topic I mind not as the original posted has moved their problem onto a new thread.

Now to actually answer the question: yes it's just personal preference, I prefer heredoc syntax as it removed the need for escaping quotes, I can enter my variables in without the need for concenation and I can keep the flow of my PHP (I tend to find going in and out of PHP means I mess up on closing braces for loops and conditionals) plus of course my HTML is nicely formateed. The most important thing, however, is to be consistent with how you deal with blocks of HTML so that debugging is easier.

Mac
Post Reply