- Colection of 65 PHP scripts for $4.29 each
“Why is my PHP not working!?” If you have ever found yourself in a situation where you needed to ask that question, you know how frustrating problems with PHP can be. Everything seems in place, you have done all you are supposed to do but still - nothing.
To help you get over this problem, in this tutorial we will go over common reasons why PHP might not be working for you as it should. We will look at a couple of cases that you might encounter in different circumstances and also provide solutions for how to get back on track. Hopefully, we will be able to help you with your issue.
PHP File Not Working on an Apache Server
The first case we want to look at is when you have a PHP file that is not executing on an Apache server. This can result in several different kinds of behavior.
Maybe when you try to open it in your browser, you get a download prompt instead. Maybe the PHP source code shows up directly in the browser window. The latter case is even worse because it may pose a security risk to boot, besides being annoying.
Either way, what is supposed to happen is not happening, which is to run the PHP script. When that is the case, you have a number of different ways of trying to address it.
Make Sure PHP Is Installed and Running Properly
When PHP is not working on your server, the first thing to do is check if it’s even present and available. You can not execute anything written in PHP if the programming language is not even installed on the server. Sounds stupid and basic but it happens.
How can you check this?
If you have access to the command line, you can simply type in php -v to see if it returns the version information of your PHP installation.
If not, and it shows any errors, this will also tell you something.
Alternatively, try uploading a file with a .php extension to your document root with the contents <?php phpinfo( ); ?>. This will display the same information as above if you access the file via browser.
Finally, you could also use a simple code snippet like <?php echo('Test'); ?>. If accessing the file through a web browser results in "Test" being displayed without the associated PHP code, this also means that PHP is working fine on your server.
If neither of these is working, it most likely means you currently don’t have PHP active on your server. In that case, check your hosting provider’s back end if there is an option for you to enable it. If that also fails, get in touch with your provider to have this sorted out.
In case you are administering your server yourself, you can follow the PHP installation guides in the official documentation. It has instructions for Linux and other Unix systems, macOS, Windows, and more.
When working with an Apache or Apache2 server and PHP is not working, the next step is to check this important file. It contains crucial directives to configure the server functionality, among them whether or not the PHP module is active.
The line you should check for looks something like this:
LoadModule php7_module "C:/local/php/php7apache2_4.dll"
Search for LoadModule PHP and see if something along those lines exists. It might be slightly different depending on the available PHP version and the operating system you are on. For example, the above is for PHP 7 on a Windows server. On a Unix system and loading PHP 8, you would have this statement:
LoadModule php_module modules/libphp.so
For PHP 5 on Ubuntu, it would be different again. You get the gist.
If you can find a line like that, make sure that it is not commented out, meaning it has no semicolon (;) in front of it. If it does not exist, you need to add it. For that, again, it’s best if you refer to the installation guides in the PHP documentation because the directive will be different in different cases.
While you have the file open, also check if the server has the PHP MIME type activated. That one tells it to run files ending in .php through the PHP parser. The respective line looks a bit like this:
AddType application/x-httpd-php .php
Search for AddType (or SetHandler for Unix), to see if it exists and that it’s not marked as a comment. If it’s not available in the file, add it.
When done, make sure to restart the Apache server for the changes to take effect.
Note: Alternatively, you can also include the above directives in a .htaccess file that lies on your server. That way, you don’t have to edit the conf file and you also don’t have to restart the server for it to take effect.
Make Sure Your PHP File Is Set Up Properly
If you are confident things are alright on your server, the next step is to make sure that the file you are trying to execute is up to par. When asking yourself why your PHP file is not opening, besides checking the MIME type, the first step is to simply look if the file ends in .php.
If it doesn’t, the server has no idea it’s supposed to parse and process it. As a consequence, the browser will not receive the right information.
If you are using the correct ending, next up, see if the file contains any short tags such as <? instead of <?php. These are not enabled on all servers, which is why using them is discouraged. In fact, they might disappear entirely in PHP 8.0. Therefore, change them to regular tags or, if you absolutely have to use them, enable the usage of short tags inside the php.ini file.
To do so, you need to find php.ini first. If you don’t know where it is, you can use the php -v command mentioned above. Just look under Loaded Configuration File to see its location. Add short_open_tag=On to it, then restart the server. However, as mentioned, these tags might soon no longer exist so it’s not the most future-proof solution.
Make Sure PHP Is Installed and Running Properly
A popular approach for development is to use a local environment. This means a virtual server that you can use to build and test projects independent of an Internet connection. It works the same as a real server without actually requiring one.
One of the most popular solutions to set this up is XAMPP. It’s name is an acronym that stands for “cross-platform Apache, MySQL, PHP, Perl”, so it has PHP already built in. However, if you find your PHP files not working in the local environment, there are a few things you can check.
Access the File Correctly
The first step is to make sure you are accessing your file over your local webserver, not just opening the file location on your hard drive with a browser.
PHP is an interpreted, server-side language. Unlike HTML and CSS, the browser does not process PHP, the server does, then sends the HTML to the browser. Therefore, if the server is not involved, the browser does not receive anything it will understand.
In the case of PHP not working in your XAMPP setup, it means making sure you are using an URL like http://localhost/index.php not a local file access such as file:///C:/xampp/htdocs/index.php.
In addition, note that in order to be able to access the file via a URL, it needs to be inside the htdocs folder of your XAMPP installation. If using a similar solution like WAMP or MAMP these tips also apply.
Double Check the Configuration Files
Aside from that, you can look at the same stuff as for a normal Apache server (since XAMPP is also just that). You can easily access files like httpd.conf and php.ini by opening the XAMPP control panel and then clicking Config in the Apache row. This will open a drop-down menu from which you can easily choose the file you want to see.
Note that in XAMPP the PHP directives are saved in the httpd-xamp.conf file, not httpd.conf. This includes the directives for AddType.
Also, use the Config menu to set up your preferred text editor for opening them. It will be easier to work with these files when you are not using the default editor your operating system comes with.
PHP Not Working in HTML
One of the advantages of PHP is that you can mix it with HTML to output web pages. However, you might find yourself in a position where you have done that but PHP is just not being executed in your HTML. What gives?
Are You Using the Right File Type?
Not to beat a dead horse but the first thing you need to understand is that, while you can mix PHP and HTML, that’s only possible inside a PHP file. You can’t simply plop down any old PHP markup inside an HTML file and expect the browser to process it flawlessly. That’s not how it works.
Only when using a PHP file does the server engage the PHP parser and will, consequently, process the PHP markup. So, in short, you can include HTML in PHP files but not PHP in HTML files (unless you give the server special directives).
Therefore, once again, if the PHP in your HTML is not working, the first thing you need to make sure is that you are using a file that ends in the .php filename extension, otherwise no dice.
Alternatively, as mentioned, you can also tell the server to treat HTML files as PHP by adding HTML to the MIME types for the PHP parser.
AddType application/x-httpd-php .html
However, this will cause the server to parse all HTML files, which can slow it down. So, maybe it’s more trouble than it’s worth
Is Your Code Marked Properly?
Of course, when using the correct file type and your PHP is still not working, the next thing is to check if maybe you are using incorrect markup. Anything that is not HTML needs to be properly marked as PHP by wrapping it into PHP tags, namely <?php and ?>.
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Otherwise, you will get an error or plain text display of your PHP markup. What really helps in these cases is use of a proper code editor with syntax highlighting. It makes it much easier to spot places where you forgot to open or close a bracket or similar small mistakes.
Are You Using Short Tags?
We already mentioned this above but not all servers are configured to understand PHP short tags. Therefore, if you find that PHP is not working in your HTML file, check if you used shorthand instead of writing it out and correct it if that is the case.
Get Your PHP Working With These Tips
When your PHP code simply refuses to work, that’s not a good day for any developer. Thankfully, the problems are usually relatively easy to troubleshoot.
It mostly comes down to checking if PHP is properly installed and the configuration files correctly set up. If you have covered those bases, it’s then a question of checking for problems with the file or markup you are using.
The above tips probably cover 99% of cases where you find your PHP not working, so we are pretty confident that, at the end of this post, you have been able to solve your particular problem.
Any other things to try when PHP is not working? If you have additional tips or questions, please let us know in the comments!