0

Why php end tags are not used?

I read somewhere and also seen the wordpress source code that after the php script ends php end tags are not used. Why it is so?

6th Jun 2021, 2:19 PM
Santosh Pattnaik
Santosh Pattnaik - avatar
7 Respostas
+ 2
Santosh Pattnaik Consider two PHP scripts. A -------- <%php echo "Hello world!"; %> -------- B -------- <%php echo "Hello world!"; -------- The first script converts the area outside the tag to HTML. Newlines in HTML are replaced by spaces so it prints "Hello world! " (13 character string instead of 12) The second script prints "Hello world!" correctly. No closing tag so nothing can be outside the closing tag. I replaced ? with % because Sololearn does not allow me to send answer that has "<?php" in it
7th Jun 2021, 5:39 AM
Ore
Ore - avatar
+ 4
Did you mean the ?> was missing (omitted)? Can you post a link to the code that was missing the end tags for verification?
6th Jun 2021, 3:20 PM
Ipang
+ 3
It is a PSR convention not to include the ending PHP tag in a file that contains only PHP statements. However, if it contains HTML embedded you should add the ending PHP tags. But in most cases, it is a bad idea to mix PHP and HTML in the same file. PHP files containing HTML are called templates and closing tags are necessary to terminate each opening PHP tag. PHP files that do not contain HTML are called scripts and the closing tag should be omitted to avoid printing whitespace accidentally.
6th Jun 2021, 5:45 PM
Ore
Ore - avatar
+ 1
Ore can you give me an example? Actually I cannot understand what kind of white space? In stackoverflow also I saw answers written about the whitespace problem but no body has given a visual example.
7th Jun 2021, 4:29 AM
Santosh Pattnaik
Santosh Pattnaik - avatar
0
U should do <? Reform ur question please
7th Jun 2021, 2:23 AM
Jad Yahya
Jad Yahya - avatar
0
Ipang check the wordpress source code, you will find this convention in almost every file.
7th Jun 2021, 4:26 AM
Santosh Pattnaik
Santosh Pattnaik - avatar
0
Ore Thanx for the explanation.I did this common mistake every day. Now I got to know about this
7th Jun 2021, 6:23 AM
Santosh Pattnaik
Santosh Pattnaik - avatar