- 5 years ago
- Zaid Bin Khalid
- 9,090 Views
-
11
Today we will learn about how to use PHP echo and PHP print statements and display the output in our browser. The basic sentence of echo and print are given below respectively.
echo ( string $arg1 [, string $... ] ) : void
print ( string $arg ) : int
The PHP echo Statement
The echo statement is an output result of one or more strings. Generally, the echo statement is used to display anything you are interested to be displayed such as variables, numbers, strings and the results of the expressions, etc. on the browser.
Hence, the echo can be termed as a language construct rather than an actual function (likewise if statement). You don’t have to write them with parenthesis e.g. echo() or echo. However, if you are required to pass multiple parameters to echo, then make sure you don’t enclose echo within parenthesis.
Show Strings of Text
The stated below example will demonstrate you on how to display text string using the echo statement:
<?php
// Displaying string of text
echo "Hello World!";
?>
The resulting output of the above PHP code can be displayed in your browser as.
Display HTML Code
In order to display the HTML code using an echo statement, you can refer to the following example that says.
<?php
// Displaying HTML code
echo "<h3 id='lcw'>This is a simple heading.</h3>";
echo "<h3 style='color: red;' class='lcw'>This is heading with style.</h3>";
?>
The resulting output of the above PHP code can be displayed in your browser as.
This is a simple heading.
This is heading with style.
Display Variables
If you want to display the variables using an echo statement then you can take a look at the following example.
<?php
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
echo $txt;
echo "<br>";
echo $num;
echo "<br>";
echo $colors[0];
?>
The resulting output of the above PHP code can be displayed in your browser as.
Hello World!
123456789
Red
The PHP print Statement
As an alternative of an echo statement, you can also use print statements to display the output on the browser. Interestingly, similar to echo the print is also a language construct not an actual function. Hence, you can use it with or without parenthesis likewise like print or print().
Typically, both statements print and echo works exactly the same with one minor difference that with the print statement you can display one string and it will return 1. Hence, the echo statement is faster as unlike print, it doesn’t return any value.
Show Strings of Text
The stated below example will demonstrate to you how to display text string using the print statement.
<?php
// Displaying string of text
print "Hello World!";
print 'Hello World!';
?>
The resulting output of the above PHP code can be displayed in your browser as.
Hello World!
Hello World!
Display HTML Code
In order to display the HTML code using a print statement, you can refer to the following example that says.
<?php
// Displaying HTML code
print "<h4>This is a simple heading.</h4>";
print "<h4 style='color: red;'>This is heading with style.</h4>";
?>
The resulting output of the above PHP code can be displayed in your browser as.
This is a simple heading.
This is heading with style.
Display Variables
If you want to display the variables using a print statement then you can take a look at the following example.
<?php
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
print $txt;
print "<br>";
print $num;
print "<br>";
print $colors[0];
?>
The resulting output of the above PHP code can be displayed in your browser as.
Hello World!
123456789
Red
- 5 years ago
- Zaid Bin Khalid
- 9,090 Views
-
11