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

Is multiple inheritance supported in PHP?

How to return JSON data to the user in Laravel?

Set PHP config Variables (like ini_set) with .htaccess

How to pick what array_intersect function to use in PHP?

What are macros in Laravel?

How to reverse htmlentities() / What is the opposite of htmlentities()?

How to get table column names from a database table in Eloquent?

What are route patterns in Laravel?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How to block IPv6 IP addresses in .htaccess