How to output (echo/print) content in PHP

PHP: How to output (echo/print) content in PHP

There are several ways to output data/content. I might have forgotten about one of them

1) A php file with any content apart from <?php . Without the PHP opening tag, everything else will be echoed out.

2) echo 'some text'; - a simple echo

3) print ('some text'); - similar to echo

4) <?='some text':?> or <?=$a_variable:?>

5) and via functions such as print_r(12345) or var_dump($something)

What is the difference between echo and print in PHP?

There isn't much difference. The only two differences are that echo does not return any value, and can take multiple arguments (echo($a,$b,$b). print() returns a value of 1 and can only take one parameter. Years ago echo() used to be a bit faster, now it would be so insignificant that it is not worth even thinking about which is faster..

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to output (echo/print) content in PHP

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to review blog posts (written by other users) before they get published on your WordPress blog?

How to check if a (blade) view file exists

How to set environmental variables in your .htaccess file

Is multiple inheritance supported in PHP?

How to block an IP range based on CIDR in Apache with .htaccess

How to increment a value in the database

How to add the CSRF (Cross-site request forgery) token in Laravel?

How to make Apache force download of files (in .htaccess)

How to install jpegtran for Linux CentOS

What are the main error types in PHP