Page 1 of 1

[Q&A] Apache won't parse php code

PostPosted: 11. February 2014 06:30
by Altrea
What can you expect from this thread?

Not parsed php code is a common issue reported several times before at this board and there are only a few reasons why this is happening.
This thread explains in detail how to identify and solve the most common reasons for this issue.

How to solve the problem?

short open tag issue

You are starting your php blocks with a short open tag
Code: Select all

Short open tags are disabled by default in the current XAMPP versions because of wrong context issues:
php.ini comment wrote:It's been recommended for several years that you not use the short tag "short cut" and
instead to use the full <?php and ?> tag combination. With the wide spread use
of XML and use of these tags by other languages, the server can become easily
confused and end up parsing the wrong code in the wrong context.

Solution: use full open tags instead
Code: Select all

protocol or request issue

You have tried to open a php file by double click, drag and drop or the open dialogue of your browser or by executing a relative link from another file (e.g. a html form) opened with one of the mentioned methods. Your Browsers address bar will show an address beginning with file:// and/or a file system path.

This will not work because your browser will not request that file from a webserver. Instead your browser uses file system functions to directly open the file.
Your browser does not have any idea that this file needs to be send through an php interpreter first to be processed.

Solution: use an correct http url. Very common it should start with http://localhost/ (localhost is a default domain name routing back to your very own computer)

file extension issue

You are trying to use php code in html files.

This will not work because by configuration Apache will not send html files through the php interpreter. Although it is technically possible to configure Apache to send every wished file extension through the php interpreter it is best practice not to do so because it will increase processing time of files that don't contain any php.

Solution: Use the file extension .php

encoding issue

You are using an encoding which is not supported by the php interpreter. This can have several sideeffects. Your editor will very common don't show any difference in your code view. Maybe you can see in the status bar which encoding the file is saved with.

I will show you a few examples of encodings and their effects.

UTF-8 with BOM
Effect: The BOM is outputted by the php interpreter and any php functions that need to be processed before any output (Cookie, Session, Redirects and other header() functions) will produce the following warning
Warning: Cannot modify header information - headers already sent by [...]

non ANSI encoding (here UCS2 big endian)
Effect: PHP will not realize the start of the PHP-Block (like the short open tag issue).

Solution: Use the correct encoding (UTF-8 without BOM or any ANSI based encoding like ISO-8859)