PHP echo
and print
statements used to display the data on the browser. These are PHP language constructors so that they will be executed quickly than any other built-in functions. These language constructors are used to display information in any format, including characters, strings, numbers, and combinations.
PHP echo Statement
The PHP echo
statement is a language constructor, so echo
can be used with/without parentheses. (i.e.) You can use echo
and echo()
to display specific data on the browser. To display multiple values, you can add comma(,
) after each information and add additional data after the comma to show on the browser. Please find below the examples,
<?php
echo "This is typical output <br>";
echo("This is another variation to display the information");
echo "This", "is", "PHP", "Tutorial";
?>
How to echo Variables in String and HTML?
We can display variables in the string and HTML using echo
statement. To render the variables inside a string, we have to use double quotes (“) instead of single quotes(‘). If we use single quotes(‘), it will display the variable name without rendering it. Please find below the examples,
<?php
$first_name = "John";
$last_name = "Doe";
echo "Welcome $first_name $last_name to our website <br>";
echo "Welcome {$first_name} {$last_name} to our website <br>";
echo 'Welcome $first_name $last_name to our website <br>';
echo 'Welcome '. $first_name .' '. $last_name .' to our website <br>';
$a = 10;
$b = 15;
echo 'Multiplication of $a and $b is :', $a * $b;
?>
Please find below the HTML examples,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1><?php echo "Welcome to PHP Tutorial"; ?></h1>
</body>
</html>
another way,
<?php
$user_name = "John Doe";
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Welcome to PHP Tutorial,'. $user_name . '</h1>
</body>
</html>';
?>
How to echo multiple lines String and an Array values?
To display string with line break using echo
statement is very quite different. We can use few different methods to display string into multiple lines and add line breaks. Also, to display array elements, we have to use a specific index of the element, or we can loop through each array element to display. Please find below the examples,
<?php
// normal echo
echo "This is sample
multiple line content
to display";
echo "<br><br>";
// using <br> tag
echo "This is sample<br>
multiple line content<br>
to display";
// using <pre> tag
echo "<pre>This is sample
multiple line content
to display</pre>";
?>
By default array
cannot be displayed using echo
statement. We will learn about the array
and its elements/index later in this tutorial series. Please check the below examples,
<?php
$fruits = array("Apple", "Orange", "Banana");
// print by using index
echo $fruits[0] .'<br>';
// loop through each element
foreach($fruits as $fruit) {
echo $fruit .'<br>';
}
// trying to echo whole array
echo "<br>Trying to echo whole array <br>";
echo $fruits;
?>
PHP print Statement
The PHP print
statement like an echo
statement and used to display output on the browser. It does return 1 as always, so it will be considered slower than the echo
statement, which doesn’t return anything during execution. The print
statement also a language constructor, and we can use print
and print()
to display information on the screen. Also, PHP print
statement cannot display multiple values by using comma(,). Please find below the examples,
<?php
print "This is typical output <br>";
print("This is another variation to display the information");
print "This", "is", "PHP", "Tutorial"; // produce error
?>
As you know already, the PHP print
statement returns 1, but echo
will not return anything. Please check the below examples to get more ideas on the concepts.
<?php
// using print
$name = "James";
$output = print $name;
echo '<br>';
echo $output;
// using echo - enable below code to see the output
/*$name = "Bond";
$output = echo $name;
echo $output; */
?>
Whatever you can able to do using echo
, print
also capable of doing the same. In the above examples, you can change print
in the place of echo
and experiment with the program to get familiar with these two language constructors.
Other useful Output Statements
print_r
– to display array elements in human readable format.
var_dump
– It used to dump information about variables.
var_export
– It returns a parsable string representation of a variables.
Please find below the examples,
<?php
$sample_array = array(1, 2, 3, array('a', 'b', 'c'));
print_r($sample_array);
print '<pre>';print_r($sample_array);print '</pre>';
var_dump($sample_array);
echo "<br><br>";
var_export($sample_array);
?>
In this tutorial, we studied echo
and print
statements to display output on the browser and other useful output statements. We will use these statements regularly in our tutorial series; let’s move to the next chapter.