![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between echo, print, and print_r in PHP?
The print and echo are both language constructs to display strings. The echo has a void return type, whereas print has a return value of 1 so it can be used in expressions. The print_r is used to display human-readable information about a variable.
Example
Let us now see an example that displays output using echo, print, and print_r:
<?php $arr = array( "John", "Jacob", "Tom", "Tim"); echo "Array...
"; foreach( $arr as $value ) { echo "Value = $value
"; } echo "
Displaying Array Values using print...
"; foreach( $arr as $value ) { print( "Value = $value
"); } echo "
Displaying Array Values using print_r...
"; print_r($arr); ?>
Output
This will produce the following output−
Array... Value = John Value = Jacob Value = Tom Value = Tim Displaying Array Values using print... Value = John Value = Jacob Value = Tom Value = Tim Displaying Array Values using print_r... Array ( [0] => John [1] => Jacob [2] => Tom [3] => Tim )
Advertisements