Syntax:
The syntax of this statement is given below.
echo (arg1, arg2… )
It can take one or more argument values for printing separated by a comma (,).
Different uses of the `echo` statement have been shown in the next part of this tutorial.
Example-1: Print simple string values using `echo`
Create a PHP file with the following script to print a simple text with a newline and without a newline. The `echo` statement does not generate a newline for the escape character, ‘\n’, but if the nl2br() function is used with the echo statement to print a text with ‘\n’ then a newline will be generated. The ‘<br/>’ tag is used with the text in the `echo` statement to add a newline in the output. The uses of ‘<br/>’ tag, nl2br() function, and ‘\n’ have been shown in the following script.
//print message with '\n'
echo 'Welcome to LinuxHint.\n';
//Print message with '<br />'
echo 'PHP is a server-side scripting language.<br />';
//Print message using nl2br() function
echo nl2br("Learn PHP for\nweb programming.");
?>
Output:
The following output will appear after executing the above script.
Example-2: Print simple variables using `echo`
Create a PHP file with the following script to know the way to print the value of a variable by using `echo` statement. A text value with a variable will be printed after concatenation after executing the script.
//Declare a variable with the string value
$text = 'The product price is ';
//Declare a variable with the number value
$price = 50;
//Print the values of the variable using `echo`
echo "$text $".$price."<br />";
?>
Output:
The following output will appear after executing the above script.
Example-3: Print array variables using `echo`
A simple variable has been printed by the `echo` statement in the previous example. Create a PHP file with the following script to know how to print the array values using the `echo` statement. One numeric array and an associative array have been defined in the script. The ‘foreach’ loop has been used in the script to print the values of the numeric with the space and print the values of the associative array in each line.
//Declare a numeric array
$flowers = array('Rose','Lily','WaterLily','Daisy', 'Aster', 'Bergenia' );
//Declare an associative array
$birds = array('Bangladesh'=>'Magpie-Robin','Australia'=>'Emu','Japan'=>'Green Pheasant','Namibia'=>'African Fish Eagle', 'United States'=>'Bald Eagle');
//Print the values of the numeric array
echo "<b>Name of the flowers:</b><br/>";
foreach($flowers as $flower)
{
echo $flower." ";
}
//Print the values of the associative array
echo "<br/><br/><b>Name of the national bird based on country:</b><br/>";
foreach($birds as $country=>$bird)
{
echo "The national bird of <b>$country</b> is <b>$bird</b><br/>";
}
?>
Output:
The following output will appear after executing the above script.
Example-4: Print string with different HTML tags using `echo`
Create a PHP file with the following script where different types of HTML tags with inline CSS has been used. Here, the ‘<p>’, ‘<font>’, and ‘<hr>’ HTML tags have been used inside the text of the `echo` statement.
//Print a text with '<p>' and '<strong>' tags
echo '<p><strong>PHP programming</strong></p>';
//Print a text with '<p>' and '<font>' tags
echo "<p><font color="red">PHP programming</font></p>";
//Print large text with '<p>' and '<font>' tags
echo "<p> <font color="blue" size="10px">PHP programming</font></p>";
//Print horizontal line using '<hr>' tag and CSS
echo "<hr style='height:2px;border-width:2;background-color:blue'>";
?>
Output:
The following output will appear after executing the above script.
Example-5: Print the output of the function using `echo`
Create a PHP file with the following script where the `echo` statement has been used inside the function and at the time of calling the function.
//Declare function for adding two numbers
function Addition($x, $y){
$output = "The sum of $x and $y is ". ($x + $y). "<br/>";
return $output;
}
//Declare a function for finding maximum value
function Maximum($a, $b, $c, $d){
echo "The maximum value among $a,$b,$c,and $d is ".max($a,$b,$c). "<br/>";
}
//Call Addition() function using values
echo Addition(25, 35);
//Initialize four numbers
$num1 = 78;
$num2 = 67;
$num3 = 96;
$num4 = 55;
//Call Addition() function using variables
echo Addition($num2, $num4);
//Call Maximum() function using variables
echo Maximum($num1, $num2, $num3, $num4);
?>
Output:
The following output will appear after executing the above script.
Example-6: Using shorthand `echo` statement
Create a PHP file with the following script where the use of the shorthand `echo` statement has been shown. Five variables have been initialized at the beginning of the script. Two values were printed by the `echo` statement, and three variables were printed using the shorthand `echo` statement in the script. The “=” is used as the shorthand `echo` statement.
$address = '17600 Newhope Street, Fountain Valley, CA 92708 USA';
$email = '[email protected]';
$contact_no = '+1 (877) 546-4786';
$website = 'https://www.kingston.com/';
?>
<p style='font-size:25px'>Company Information</p>
<?php
//Print two variables using normal `echo` statements
echo "<p>Name: $company</p>";
echo "<p>Address: $address</p>";
?>
<!--Print variables using shorthand `echo` statements -->
<p>Email: </p>
<p>Contact No.: </p>
<p>Website: </p>
Output:
The following output will appear after executing the above script.
Conclusion:
The `echo` statement makes the print task easier than other printing options of the PHP. Different uses of the `echo` statement have been described in this tutorial by using various PHP examples for helping the PHP users to use this statement properly in their script.