- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- articles and Answers
- Effective Resume Writing
- HR Interview articles
- Computer Glossary
- Who is Who
What is the difference between echo, print, and print_r in PHP?
echo: echo is not a function rather it is described as a language construct. It accepts an list of argument (multiple arguments can be passed) and returns no value or returns void. It cannot be used as a variable function in PHP. It is used to display the output of parameters that is passed to it. It display the outputs one or more strings separated by commas.
Example:
<?php
// Code by Codegyan
// PHP program to illustrate echo
// Declare variable and initialize it.
$x = "Codegyan ";
$y = "Computer science portal";
// Display the value of $x
echo $x, $y;
?>
Output:
Codegyan Computer science portal
print: It is not a real function. it is a language construct but always returns the value 1. So it can be used as an expression. Unlike echo, print accepts only one argument at a time. It cannot be used as a variable function in PHP. The print outputs only the strings. It is slow compared to that of echo.
Example:
<?php
// PHP program to illustrate echo
// Declare variable and initialize it.
$x = "Codegyan";
// Display the value of $x
print $x;
?>
Output:
Codegyan
print_r(): print_r() is a regular function. It outputs the detailed information about the parameter in a format with its type (of an array or an object), which can be easily understandable by humans. In this function the output get stored on the internal buffer when the return parameter is passed. If pass the return parameter to TRUE, print_r() would return the complete information rather than just print it. During walk-through this function helps in identifying any of the glitches while executing the program. It is more similar to the var_dump() function.
Example:
<?php
// PHP program to illustrate echo
// Declare an array
$arr = array('0' => "Codegyan",
'1' => "Computer",
'2' => "Science",
'3' => "Portal");
// Display the value of $x
print_r($arr);
?>
Output:
Array
(
[0] => Codegyan
[1] => Computer
[2] => Science
[3] => Portal
)
Example:
<?php
$a = "Codegyan";
$b = array('0' => "Code", '1' => "Gyan" );
$c = 3.14;
$d = 7;
// Single argument
print "\n$a\n";
// Multiple argument
echo $c + $d . "\n";
// Return with internal output buffering
print_r($b);
?>
Output:
Codegyan
10.14
Array
(
[0] => Code
[1] => Gyan
)
- Related Questions & Answers
- How to Pass JSON Data in a URL using CURL in PHP ?
- Create Function to Insert Post using core PHP
- What are Python function attributes?
- How to Get All Data of Particular ID and Display using Core PHP 8
- How To Clean Up Unnecessary Code From WordPress Header Without Plugins
- Write PHP Program To Check Prime Number
- Add and Delete Images from the Server in TinyMCE editor using PHP
- Convert png, jpg, jpeg, gif to webp using PHP Function
- How can we create recursive functions in Python?
- Write PHP Program To Print Sum Of Digits
- How to Create Function to Get Post Metadata Using PHP to Get Value by Key
- How to Insert Image to Servers using TINYMCE Editor Using PHP
- PHP Date and Time Function
- Write Steps to create a functional and secure API using PHP
- How to echo HTML in PHP ?
- How to Create an Array for JSON using PHP ?
ads