Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - 'print/echo' displaying closing tags - or not outputting

Tags:

php

Starting some PHP and confused by the way echo/print are working.

I've got this code in my index.html:

<?php
Print '<div>Hello, World!</div>';
?>

And the output on my page is

Hello, World!'; ?> 

If I remove the <div> tags, I get no output. using echo produces the same behaviour.

What's going on? Everything I find on Google makes it seem straight forward.

like image 769
Exbi Avatar asked Dec 17 '11 23:12

Exbi


People also ask

Does PHP require a closing tag?

Note: The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later.

Can we use print instead of echo in PHP?

echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions.

What is the PHP closing tag?

The standard closing tag is: ?> These tags can be used to jump in and out of "PHP mode" any number of times in a PHP file, including a PHP file containing HTML elements.

Where does PHP echo output go?

Echo sends output to the output buffer, If it's embedded in HTML (which is also being sent to the output buffer) then it appears that it writes that to the "source".


1 Answers

You cannot put PHP code in an HTML file (unless you tell Apache to parse HTML files).

Rename the file to index.php

Do you have a webserver with PHP set-up?

like image 200
Richard Avatar answered Sep 24 '22 01:09

Richard