Code has been added to clipboard!

Use of PHP echo and print Statements Explained in Useful Code Examples

Reading time 3 min
Published Jan 6, 2016
Updated Sep 30, 2019

PHP echo and print statements are language constructs and not functions. You are free to use them without parentheses.

Both of these constructs are for outputting strings. Differences between print and echo constructs are minor but echo can be seen as a more time-efficient option.

Additionally, echo can accept several parameters, while print can be used as an expression since it can return a value of 1.

PHP echo and print: Main Tips

  • PHP print and echo statements are one of the most commonly used constructs to output data (text, variables, etc.).
  • You don't need to use parentheses with them since they are technically not functions.
  • print_r is a function, mostly used for debugging.

echo Language Construct: Proper Use

By using PHP echo, you can print one or multiple strings. Let's look at a PHP example of how you can achieve that:

Example
<?php  	
  echo "<h2>Am I ready?!</h2>";  	
  echo "Howdy!<br>";
  echo "I'm ready!<br>";  	
  echo "I ", "made ", "this ", "string ", "with my handy hands!";  
?>

In the code below, you can learn how to display both text and values of variable values by using echo. You can see 4 different variables in this PHP example: $text1, $text2, $var1 and $var2:

Example
<?php
  $text1 = "I am ";
  $text2 = "Ready!";  	
  $var1 = 4;
  $var2 = 3;
  echo "<h2>" . $text1 . $text2 . "</h2>";
  echo $var1 + $var2;
?>

Note: when you assign parameters to echo, you need to write them within parentheses.

How print Is Used

This PHP example below shows how you can display text using PHP print and include HTML markup into the statement:

Example
<?php
  print "<h2>I am ready to learn PHP!</h2>";  	
  print "Hey there!<br>";
  print "I will learn ALL the PHP!";  
?>

The code of the PHP example below shows how you can display both text and a PHP variable using PHP print. Again, you can see four variables being used and their values outputted:

Example
<?php
  $text1 = "Learning PHP";
  $text2 = "ALL the PHP";  	
  $var1 = 58;
  $var2 = 4;  	
  print "<h6>" . $text1 . $text2 . "</h6>";
  print $var1 + $var2;
?>

How print_r Is Different from print and echo

The use cases of echo, print and print_r in PHP are different.

echo and print output strings. print_r in PHP returns details about variables in a more human-readable form. It provides data type of variables. When used on arrays, print_r returns all the elements in the list.

In this code example, we are making PHP print arrays by using the print_r function:

Example
<?php
$a = array ('a' => 'airplane', 'b' => 'train', 'c' => array ('o', 'k', 'f'));
print_r ($a);
?>

Note: to show information in multiple lines, apply \n with the print_r. Also, include <pre> to indicate several lines.

PHP echo and print: Summary

  • print and echo PHP statements are for displaying text or variables.
  • Parentheses are not required with these language constructs.
  • print and echo output information to the screen, while print_r in PHP shows information about variables in a more human-readable way.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()