Omitting php tag closure

Possible duplicate:
Why do some scripts omit the closing php tag? > '?

I read some articles on how to omit PHP closing tags, as they say it's good PHP programming practice if your .php file does not contain any other things. There are many such questions, but after I tried what they have done so far, it worked well on my machine. Maybe this is a problem or something else?

But I don’t quite understand why this can be a good programming practice, since it brings space or something like that, but I tried it and worked very well.

Master.php

<?php echo "Master.php"; include "Slave.php"; header("Location:Slave.php"); ?> 

Slave.php

 <?php echo "Slave.php"; ?> 

I really don't quite understand what the problem should be if I didn't use the closing php tag.

Thanks.

+6
source share
4 answers

The main problem is that you can include extra spaces (but it can be any characters) after closing ?> (Except for the one \n that PHP allows, thanks Mario ).

This extra space appears PHP as an output to send. This forces PHP to start sending the response body, making it impossible to set / change any additional headers.

This is difficult to debug (since a text editor usually has an invisible space), and often the reason for the terrible headers has already sent an error.

+10
source

The problem with the closing tag is that any spaces after the last? > can cause errors and is very difficult to detect when fixing errors.

+2
source

It is usually best NOT to end your script with a closing PHP tag.

In your case, the space may remain after ?> (It’s very difficult and difficult to say where the error is if something breaks because of this), so it will be considered as a result, and you won’t be able to start the session, for example, or pass the headers to in case you are developing a website.

Just my opinion. I probably never finished my closing tag scripts

0
source

The problem arises because there are any number of line breaks other than 1: some php parsers get upset if there is no new line in this, but if you have more than one, it prints because everything that is outside of php tags considered HTML.

The most common problem is that the library / model file will have an extra line break, causing the headers to be sent long before the page / view is created.

0
source

Source: https://habr.com/ru/post/889504/


All Articles