Can inconsistent use of PHP tags cause white screen / server error?

I WAS inconsistently using PHP end tags.

Got white screen on FF and 400 or 500 error (can’t remember which) on Chrome.

Stumbled onto inconsistent use of PHP end tags as the possible problem by googling.

Now have policy for using PHP end tags: no PHP end tags unless file contains HTML.

Problem fixed except I don’t know for a fact, that inconsistent use of PHP tags caused the problem

How likely is it that an nconsistent use of PHP tags cause white screen / server error?

Is no PHP end tags unless file contains HTML a good policy?

If so, why?

Sometimes it can. Really depends if you have error logs enabled or not. Really helps determine if it’s syntax error or inconsistency. Also, sometimes, blank pages can occur from not doing anything with non-existing data. Such as pulling data from your database and then not doing anything if the data doesn’t exist.

2 Likes

What’s your policy for using PHP end tags?

I don’t use them if I don’t have to. If there is no more outout, I don’t end my PHP tags. White spaces can be a big pain in the rear if you don’t know where the problem begins.

It’s pretty commonly accepted as good practice. I’ve been coding my PHP that way for years and not run into any problems.

It’s recommended in the PHP manual, and also in popular coding standards such as PHP-FIG’s PSR-2.

2 Likes

Similar to what you mention. For all PHP controller scripts I have taken to omitting the closing tag, not that it should matter in most cases.
Then in my HTML templates, where the bulk of code is html, with odd bits of php inserted here and there, I obviously need closing tags.

Error reporting is the one to tell you that. That is the point @spaceshiptrooper is making.
Whether you have on-screen error reporting enabled of your local dev environment, or errors recorded to the log on the live server, those white screens should come with some explanation which will help you identify the source of the problem.

One of the reasons (trying to recall) is you avoid problems from having any unnoticed whitespace after the tag, which could have potential to to cause problems with headers or sessions, as the must be no output to the browser before them.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.