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

What are Apache MPMs?

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How to get the first row that matches some where queries, or create it if it doesn't exist (in Laravel's Eloquent)?

How to Enable Directory Listings in Apache with .htaccess

How to add comments in PHP

How to check if a relation was loaded on an Eloquent model already?

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

How to return an item in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

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

Why should you update APP_URL from from http://localhost?