Fix: PHP being displayed in browser / source code.

If your PHP code is being displayed in the browser, it means that your server has not been setup to serve PHP scripts. Here are a list of things that you need to check in order to debug the issue.

Firstly, make sure that you are saving your PHP files in UTF-8. In the past, a number of developers have come across an issue where saving the file in other charsets has led to their opening and closing PHP tags being ignored.

Failing that, there are a number of other questions that you need to ask yourself!

Is PHP installed?

Is PHP actually installed? A lot of servers will only serve HTML files by default. More often than not, you will need to install PHP and configure it to work with your web server. If you do not have control over the server that you are uploading your files to, then you will need to contact your hosting provider. On a number of occasions, I’ve come across lower-end hosting packages that do not support any server-side scripting languages (i.e. HTML-only). Make sure that this isn’t the case with the package that you are using. If you do have control over the server, then you will need to make sure that PHP is properly installed. If you search around, you will find hundreds of tutorials on how to setup PHP and Apache / Nginx to work on Windows, Linux and Macintosh.

Misconfigurations.

If PHP is installed and your code is still being displayed in the browser, then I am willing to bet that you have fallen victim to a simple misconfiguration. More often than not, this kind of issue can be solved by uncommenting a line in a configuration file. For example: In Apache’s httpd.conf file, you will need to make sure that the line “LoadModule php5_module” has been uncommented and that there is no semi-colon (;) at the beginning of the line.

Restart your web server.

If you’re pretty sure that you have set everything up and that you haven’t fallen victim to a misconfiguration, then you should make sure that you have restarted your web server.
More often than not, you will need to restart Apache or Nginx for the changes to take effect.

.php

If you have done all of that and your code is still being displayed in the browser, then you will need to make sure that your files are using the .php extension! PHP code will not be executed if it is saved inside a .html file!

Run it through your web server!

Lastly – You will need to make sure that you are actually running your files via a web server! A lot of beginner PHP developers will make the mistake of opening the page via their file system, instead of putting it into the root of a PHP-enabled webserver. Although this can be done with regular HTML, it will not work with PHP!

Hopefully, this article helped you to solve the issue!

Comments

comments