Print html code in php

A fundamental use of the PHP programming language is to build dynamic web applications. Hence, we often need to print information in HTML inside a PHP web application.

This guide will learn about two main ways to print HTML content on a PHP web application.

PHP Echo Function

If you know the basics of PHP and get output, you know about the echo method. It is used extensively in PHP to output values on the screen. Although the echo method does not behave like a function, more like a constructor, you can add parenthesis to the statement if required.

Like any other valid PHP statement, you terminate it with a semi-colon. The echo method will take the content inside the quotes and print it to the screen.

You can use the echo statement to print strings, variables, results from expressions and functions, and more.

The examples below show how to use the echo statement.

The above examples contain multiple statements that can print strings, numbers, and variable interpolation.

If you want to print multiple statements, you can specify each of them separated by comma as shown in the example below:

To print HTML markup using the echo statement, we can pass the content to it as shown in the example below:

The code above will print valid HTML content.

PHP Print Function

The other method we can use to print HTML markup in PHP is the print function.

It is similar to echo, except it behaves like a standard PHP function. It accepts a single argument meaning it can print a single string.

The following is a simple example of the print method.

Like echo, you can output strings, numbers, and variables using the print statement.

To print HTML content using the print statement, you can do:

The above should process the markup and display it accordingly.

If you want to learn how to print PHP values and variables inside HTML, check our tutorial on the topic.

Closing

This tutorial taught you how to print HTML markup inside a PHP file. This helps make the web page dynamic as you can fetch information from the server and display it on the browser.

About the author

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

I have HTML tags present inside php variable. I want to print the values as it is. Here is what i tried.

$str = "A 'quote' is bold";

echo htmlentities[$str];
// Outputs: A 'quote' is <b>bold</b>

echo $str; //out puts as follows

A 'quote' is bold

But i want to print it as

 A 'quote' is bold

Also, Is there any setting can be done at the TOP of the php page, so that i dont need to use it in every php variables ?

asked Feb 23, 2014 at 12:38

7

Just use

 $str = "A 'quote' is bold ";

echo htmlspecialchars[$str];

because htmlentities convert some characters to HTML entities.

You should instead use htmlspecialchars

It replaces characters as below:

'&' [ampersand] becomes &
'"' [double quote] becomes " when ENT_NOQUOTES is not set.
"'" [single quote] becomes ' only when ENT_QUOTES is set.
'' [greater than] becomes >

You can check php fiddle here

answered Feb 23, 2014 at 12:43

ShivaShiva

6,4744 gold badges34 silver badges59 bronze badges

6

This should work -

$str = "A 'quote' is bold";
echo "".$str."";
//Outputs - A 'quote' is bold

EDIT:

is deprecated as was pointed out in the comments, and this seems to be the workaround for it[using the

 tag and htmlentities]-

$str = "A 'quote' is bold";
echo "
".htmlentities[$str]."
"; //Outputs - A 'quote' is bold

answered Feb 23, 2014 at 12:51

KamehamehaKamehameha

5,3731 gold badge22 silver badges27 bronze badges

5

If you want to print HTML, don't use htmlentities.

If this is user input, you should still filter it.

Edit:

If you want the browser to show the text as A 'quote' is bold, htmlspecialchars or htmlentities is the correct function to use as they escape the HTML code and the browser will show the tags as you want.

BenMorel

32.7k48 gold badges170 silver badges302 bronze badges

answered Feb 23, 2014 at 12:41

max-mmax-m

8277 silver badges9 bronze badges

3

to make it as setting..


answered Feb 23, 2014 at 13:43

loganlogan

7,55636 gold badges108 silver badges181 bronze badges

Can I write HTML code in PHP?

While HTML and PHP are two separate programming languages, you might want to use both of them on the same page to take advantage of what they both offer. With one or both of these methods, you can easily embed HTML code in your PHP pages to format them better and make them more user-friendly.

How do I print HTML code?

Printing an HTML Document.
Open a document in the HTML editor..
Do one of the following: On the main menu, click File > Print. Press CTRL+P. The document prints as it appears in the HTML editor, NOT as it appears in a browser..

HOW include HTML code in PHP?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.

How do I print a PHP script?

The echo command is used in PHP to print any value to the HTML document. Use tag inside echo command to print to the console.

Chủ Đề