Formatting PHP Text

Formating PHP Text Using HTML

woman programming on computer
Georgijevic/Getty Images

So you've gone through the PHP tutorials or are new to PHP in general, and you can make some nifty things in PHP, but they all look like plain text. How do you jazz them up?

Formatting PHP text isn't done with PHP; it's done with HTML. You can do this in two ways. You can add HTML inside the PHP code or you can add the PHP code inside the HTML. Either way, the file must be saved as a .php or another file type that is allowed to execute PHP on your server.

Changing PHP Text Color Using HTML Inside PHP

For example, to change the PHP text color to red.

 Hello World!";

In this case, the hex color number #ff0000 sets the PHP text that follows it to red. The number could be replaced by other hex color numbers for other colors. Notice the HTML code is located inside the echo. 

Changing PHP Text Color Using PHP Inside HTML

The same effect is achieved with the following code, which uses PHP inside HTML.

In the second example, a single line of PHP is inserted inside the HTML. Although here it is only a line to make the text red in this example, it could be inside a fully formatted HTML page to get any look you want.

Types of Formatting Available in HTML

It is easy to make text formatting changes to PHP text inside HTML. Although many of these formatting commands have been suspended in Cascading Style Sheets, they all still work in HTML. Some of the text formatting commands that can be used include:

  • Bold -
  • Italic -   
  • Underline - 
  • Strike-out -  or 
  • Small - 
  • Font size -  , replace the ? with a number from 1 to 7, 1 being the smallest
  • Center text - 

A complete list of text formatting tags is available.

mla apa chicago
Your Citation
Bradley, Angela. "Formatting PHP Text." ThoughtCo, Aug. 27, 2020, Bradley, Angela. (2020, August 27). Formatting PHP Text. Retrieved from Bradley, Angela. "Formatting PHP Text." ThoughtCo. (accessed March 27, 2023).