Website maintenance

PHP error "unexpected end of file

Table of contents

With many a WordPress project, you find yourself in the situation of designing your own templates. WordPress templates are typical representatives of the old PHP school, according to which PHP is already its own template engine and thus form a hybrid of HTML and PHP instructions. This is more or less how PHP came into being. In the meantime, PHP and HTML code are separated by additional template engines such as the oldie but goldie Smarty or Twig and Blade (Laravel), which makes sense for larger projects, especially for reasons of reusability, but also clarity of the project. Nevertheless, WordPress does without template engines and leaves the separation of logic and presentation to the developer. The result is, for example, code expressions of the form:

“Leider” funktioniert aber meistens auch die Kurzversion, indem man <?php durch <? ersetzt, also:

Viele Apache Webserver werden an dieser Stelle den Code brav ausführen. Das liegt daran, dass in der PHP.ini die Konfigurationsvariable “short_open_tag” den Wert “On” besitzt, was die Ausführung von dieser Syntax erlaubt. Problematisch wird es allerdings wenn die PHP Version den Wert “Off” hat. An dieser Stelle wird es einen Fatalen Fehler geben, z.B.:

“PHP Parse error: syntax error, unexpected end of file in …”

Das Problem dabei ist, dass viele Entwicklungs-Umgebungen, wie z.B. “MAMP”, die kurze Schreibweise per Default erlauben. Auf dem Produktionsserver des Hosters kann das dann zu bösen Überraschungen führen – und wer öfter mit WordPress arbeitet, weiß, dass das Debugging nicht unbedingt zu den Stärken dieses CMS gehört.

Entwickler, die z.B. PHPStorm benutzen, können bei Inspections > PHP > Code Style die Option “Short Open Tag Usage” anhaken und bekommen dann während der Entwicklung schon eine entsprechende Warnung:

Happy WordPress Coding!

One more hint: Most likely you will not see the error message on the screen, but instead simply a white screen of death will be displayed. However, you will find such a message somewhere in the error logs of your hoster. Pay attention to the PHP files that you have modified or created.

 

See also

More on this topic

Help, my Firefox, Chrome, Safari, Edge shows me old or broken content

After changes have been made as part of website maintenance, we often receive feedback from customers such as "Everything still looks the same as before" or "But now the website is completely broken!" What's going on...

To the article...

Sequence for WordPress updates

For the secure operation of a Wordpress site, it is necessary to regularly update both the basic system and the extensions. For reasons of operational safety, it is advisable to follow a certain sequence of steps.

To the article...

What fonts are used for headings and text on our site?

"That looks nice, I wonder what font that is? And what colour?" In web development, as in the garden, it's always interesting to look at the results of other creators....

To the article...

How to make a screenshot of the complete homepage

Gerry the web gardener answers the question how to make a screenshot of a complete homepage.

To the article...