The echo statement can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions etc.
The echo statement can be used with or without parentheses: echo
or echo()
.
Displaying Text
Below is an example of using the echo
statement to output text.
<?php
// Displaying string of text
echo "Hello World!";
Displaying HTML Code
Below is an example of displaying HTML code using the echo
statement.
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<br>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
echo "<br>";
Displaying Variables
Below is an example of displaying variables using the echo
statement.
<?php
$myTxt = "Hello World!";
$myNum = 123456789;
$myColors = array("Red", "Green", "Blue");
// Displaying variables
echo $myTxt;
echo "<br>";
echo $myNum;
echo "<br>";
echo $myColors[0];
echo "<br>";
The print statement
The print
statement works the same as the echo
statement. It can also be used with or without parentheses. The following code provides examples in using the print
statement.
<?php
// Displaying string of text
print "Hello World!";
echo "<br>";
// Displaying HTML code
print "<h4>This is a simple heading.</h4>";
print "<br>";
print "<h4 style='color: red;'>This is heading with style.</h4>";
print "<br>";
$myTxt = "Hello World!";
$myNum = 123456789;
$myColors = array("Red", "Green", "Blue");
// Displaying variables
print $myTxt;
print "<br>";
print $myNum;
print "<br>";
print $myColors[0];
print "<br>";
Exercise
Using the echo
or print
statement, print the word "Hypertext" in blue letters.
<?php
echo "Hello World!";
<?php
echo "<h4 style='color: blue;'>Hypertext</h4>";
{
"test_output_contains":{
"expected":"Hypertext"
},
"success_message":"Good job!",
"error_message":"Please read the instructions again."
}