If your browser is displaying PHP code, it means that your server has not been setup to serve PHP scripts. Here is a list of things that you need to check in order to fix this issue.
Firstly, make sure that you are saving your PHP files in UTF-8.
In the past, a number of developers have reported an issue where saving the file in other charsets 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?
Did you install PHP?
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 have come across lower-end hosting packages that do not support any server-side scripting languages. In other words, they were 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.
If PHP is installed and your code is still showing in the browser, then you may 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 everything has been setup correctly and 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.
This is because they only read the configuration file on startup.
Are you using the .php extension?
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.
Well, not unless you’ve specifically configured your web server to do so.
Make sure that you are running 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 make the mistake of opening the page via their file system instead of putting it into the root of a PHP-enabled web server.
Although you can do this with regular HTML, it will not work with PHP.
Typically, this means that you will need to access it via http://localhost.