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.
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.
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.
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.
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".
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With