I have a php file that contains two include statements. The first one works fine, but the second refuses to "include" and only results in the html <!–#include… statement being seen in the page source code instead of actually including the code from the referenced file.
truncated code is arranged as follows:
some html stuff
<?php include 'menusZ.htm';?> (this statement works fine
and produces a menu on the page)
some more html
some more php code
some more html code
<?php include 'footerZ.htm';?> (this statement does not produce the desired footer...only the above mentioned <!--#include file="footerZ.htm" --> when the generated page source is viewed.
Notice that the syntax is exactly the same for the 2 include instances.
The calling php file and the two files to be "included" are all in the same directory.
Any ideas on why the 2nd include is not being realized would be appreciated.
2
Answers
I found the issue, fixed it, and will answer my own question.
The issue was in the area of "some more php code" which was code that was not mine but which was needed for the page. There was a "return;" statement in the main script (with lots of additional code following it for a different processing thread) which obviously terminated all processing for the page, so the final include was not being processed. I replaced the "return;" with a "goto label:" and placed the label: at the end of the script just before the php script closing "?>". This allowed the script to just end normally without terminating all processing of the page, and the 2nd include worked fine.
As there was some mention of syntax, I will say that there are numerous ways to successfully include another file and I won't dare to say that one way is the correct way. In the process of my troubleshooting, I had tried:
They all work, not to mention the require, include_once, etc. variations of including a file.
And no...I am not an AI agent!
It seems like you are encountering an issue with Server-Side Includes (SSI) not being processed properly for the second include statement. In PHP, you use include to include PHP files, but is an SSI syntax, not PHP.
The reason the first include () works fine is that PHP recognizes the include statement and includes the contents of menusZ.htm as expected. However, for the second include (), PHP won’t be able to process it because it’s not a valid PHP include statement.
To include the contents of footerZ.htm using PHP, you should use the regular PHP include syntax for both includes:
Make sure that both menusZ.htm and footerZ.htm contain valid HTML code. PHP include will treat the files as plain HTML and include their content accordingly.
If you want to use Server-Side Includes (), you’ll need to configure your web server to process SSI directives. This is typically done by enabling SSI in the server configuration or creating an .htaccess file with the necessary SSI configuration directives.
However, since you mentioned that the files and the calling PHP file are all in the same directory, I recommend sticking with the PHP include as it is more commonly used for this purpose and should work as expected once you remove the SSI syntax from the second include statement.