top of page

PHP | echo and print

Updated: Dec 2, 2019



We have seen echo statement quite frequently in PHP codes of previous articles.It is the most basic way for displaying output in PHP.

However, there are two basic ways to get output in




PHP:-


1- echo

2- print


PHP echo statement

In PHP ‘echo’ statement is a language construct and never behaves like a function, hence no parenthesis required. The end of echo statement is identified by the semi-colon (‘;’). We can use ‘echo’ to output strings or variables. Below are some of the usage of echo statement in PHP:

1- Displaying Strings: We can simply use the keyword echo followed by the string to be displayed withing quotes. Below example shows how to display strings with PHP:


<?php

echo "Hello,This is a display string example!";

?>


Output:

Hello,This is a display string example!

2- Displaying Strings as multiple arguments: We can pass multiple string arguments to the echo statement instead of single string argument, separating them by comma (‘,’) operator. For example, if we have two strings say “Hello” and “World” then we can pass them as (“Hello”,”World”). Below example shows how to do this:

<?php

echo "Multiple ","argument ","string!";

?>

Output:

Multiple argument string!

3- Displaying Variables: Displaying variables with echo statement is also as easy as displaying normal strings. Below example shows different ways to display variables with the help of PHP echo statement:-

<?php

//defining the variables

$text = "Hello, World!";

$num1 = 10;

$num2 = 20;

//echoing the variables

echo $text."\n";

echo $num1."+".$num2."=";

echo $num1 + $num2; ?>

Output:

Hello, World!
10+20=30

PHP print statement


The PHP print statement is similar to the echo statement and can be used alternative to echo at many times.It is also language construct and so we may not use parenthesis : print or print(). The main difference between the print and echo statement is that print statement can have only one argument at a time and thus can print a single string. Also, print statement always returns a value 1. Like echo, print statement can also be used to print strings and variables. Below are some examples of using print statement in PHP:


1- Displaying String of Text:

We can display strings with print statement in the same way we did with echo statements. The only difference is we can not display multiple strings separated by comma(,) with a single print statement. Below example shows how to display strings with the help of PHP print statement:-


<?php

print "Hello, world!";

?>

Output:

Hello, world!

2- Displaying Variables:

Displaying variables with print statement is also same as that of echo statement. The example below shows how to display variables with the help of PHP print statement:-

<?php

//defining the variables

$text = "Hello, World!";

$num1 = 10;

$num2 = 20;

//echoing the variables

print $text."\n";

print $num1."+".$num2."=";

print $num1 + $num2;

?>

Output:

Hello, World!
10+20=30

  
 
 
 

68 Comments


Thank you for sharing this helpful article. It gave me a fresh perspective and encouraged me to keep learning. While browsing grant pharmacy, I found useful information. I also wanted to learn more about Antivert. One question remains: is doxycycline for uti a common treatment option? Thank you.


Like

rr88 com mình thấy bạn bè nhắc vài lần nên tiện tay vào lướt thử cho biết thôi. Không có chơi gì nhiều, chủ yếu xem trang chủ họ làm ra sao. Cảm giác đầu tiên là giao diện nhìn khá thoáng, mấy phần nội dung chia thành từng khối nên kéo xuống không bị ngợp chữ. Mình để ý nhất là đoạn họ nói về thanh toán xử lý nhanh, kiểu nạp rút “siêu tốc”, đọc cái là nhớ liền vì trước giờ mình hay bị ám ảnh vụ chờ lâu ở mấy chỗ khác. Với lại phần ưu đãi họ trình bày gọn gàng, không kiểu nhét cả đống câu chữ liền tù tì. Nói chung lướt vài…

Like

MM 88 mình vừa lướt thử lúc rảnh, kiểu nghe mọi người nhắc nhiều nên vào xem giao diện ra sao thôi. Ấn tượng đầu là trang nhìn thoáng, chia khối rõ ràng nên không bị ngợp, kéo xuống cũng dễ theo dõi. Mình hay khó chịu mấy site nhét chữ dày đặc, mà ở đây nhìn có khoảng thở nên mắt đỡ mệt. Cái mình thích nhất là thanh menu để khá nổi, bấm qua lại mấy mục không phải tìm lâu, cảm giác điều hướng mượt. Với lại mấy phần danh sách trình bày theo cột nên lướt nhanh vẫn nắm được, không bị rối. Nói chung mình vào xem vài phút mà thấy ổn vì bố cục…

Like

XX 88 mình mới ghé thử vài phút thôi, chủ yếu coi trang có dễ nhìn không vì thấy mọi người nói nhiều. Vào cái là thấy họ chia nội dung theo kiểu từng khối rõ ràng, kéo xuống không bị loạn mắt. Mình để ý có phần tin tức cập nhật đặt tiêu đề khá nổi, nên muốn xem cái gì mới cũng dễ định hướng. Với lại cách họ trình bày mấy bài hướng dẫn nhìn gọn, chữ không bị dày đặc nên đọc lướt vẫn hiểu đang nói về gì. Mình không đào sâu game hay gì đâu, chỉ thấy giao diện làm người mới đỡ ngợp. Nói chung cảm giác như họ chăm chút phần sắp…

Like

RR888 hôm trước mình lướt thử vì thấy mọi người nhắc, kiểu vào xem giao diện ra sao thôi chứ không có ngồi đọc kỹ từng thứ. Cảm giác đầu tiên là trang nhìn sáng sủa, bố cục chia khối khá rõ nên kéo xuống một chút là nắm được mình đang ở phần nào. Mình thích nhất chỗ họ sắp xếp danh sách theo dạng cột, nhìn gọn và dễ quét mắt, không bị dồn chữ hay rối như vài web mình từng gặp. Với lại cái menu để ngay chỗ dễ thấy nên chuyển qua lại mấy mục nhanh, không phải mò lâu. Nói chung dùng kiểu “mở lên là hiểu”, vì các khối nội dung trên trang…

Like

REALCODE4YOU

Realcode4you is the one of the best website where you can get all computer science and mathematics related help, we are offering python project help, java project help, Machine learning project help, and other programming language help i.e., C, C++, Data Structure, PHP, ReactJs, NodeJs, React Native and also providing all databases related help.

Hire Us to get Instant help from realcode4you expert with an affordable price.

USEFUL LINKS

Discount

ADDRESS

Noida, Sector 63, India 201301

Follows Us!

  • Facebook
  • Twitter
  • Instagram
  • LinkedIn

OUR CLIENTS BELONGS TO

  • india
  • australia
  • canada
  • hong-kong
  • ireland
  • jordan
  • malaysia
  • new-zealand
  • oman
  • qatar
  • saudi-arabia
  • singapore
  • south-africa
  • uae
  • uk
  • usa

© 2023 IT Services provided by Realcode4you.com

bottom of page