PHP echo and print

Back to ACP page

Table of Contents

PHP echo Statement

The echo statement can be used with or without parentheses: echo or echo().

<h4>echo_Statement</h4> <p>The echo statement can be used with or without parentheses: echo or echo().</p> <?php echo "Hello"; //same as: echo ("Hello"); ?>

Example 1

Result View Example

PHP Display Text

In the example below, we use the PHP echo statement to output the text "Hello World!".

<h2>phpTeeratus</h2> <p>From w3schools.com, Experiment by Teeratus_R </p> <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?>

Example 2

Result View Example

PHP Display Variables

In the example below, we use the PHP echo statement to output the text "Hello World!", but we use a variable to output the text:

<h4>Display_Variables</h4> <p>Display Variables</p> <hr> <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; echo "<h2>$txt1</h2>"; echo "<p>Study PHP at $txt2</p>"; ?>

Example 3

Result View Example

PHP Using Single Quotes

<h4>Using_Single_Quotes</h4> <p>From w3schools.com, Experiment by Teeratus_R </p> <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; echo '<h2>' . $txt1 . '</h2>'; echo '<p>Study PHP at ' . $txt2 . '</p>'; ?>

Example 4

Result View Example

PHP print Statement

<h4>phpTeeratus</h4> <p>From w3schools.com, Experiment by Teeratus_R </p> <?php // The PHP print Statement print "Hello"; //same as: print("Hello"); ?>

Example 5

Result View Example

PHP print Display Text

The following example shows how to output text with the print command

<h4>print_Display_Text</h4> <p>From w3schools.com, Experiment by Teeratus_R </p> <?php print "<h2>PHP is Fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>

Example 6

Result View Example

PHP Print Display Variables

<h4>Print Display Variables</h4> <p>The following example shows how to output text and variables with the print statement:</p> <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; $x = 5; $y = 4; echo "<h2>" . $txt1 . "</h2>"; echo "Study PHP at " . $txt2 . "<br>"; echo $x + $y; ?>

Example 7

Result View Example

PHP print Using Single Quotes

Strings are surrounded by quotes, but there is a difference between single and double quotes in PHP.
When using double quotes, variables can be inserted to the string as in the example above.
When using single quotes, variables have to be inserted using the . operator, like this:

<h4>print_Using_Single_Quotes</h4> <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; print '<h2>' . $txt1 . '</h2>'; print '<p>Study PHP at ' . $txt2 . '</p>'; ?>

Example 8

Result View Example

Document

Document in project

You can Download PDF file.

Reference