This question already has answers here:
Efficiency for including files of functions (in PHP)
(5 answers)
Closed 5 years ago.
If I use PHP's include function to import parts of the page that are repeated like navigation, header and footer for example - will that make the code more or less efficient or would it be the same?
Thanks
The include()-function reads the entire content of the file you are including, then execute it and then paste the result right where it is called. So regarding code speed, it would be less efficient. However, if you don't include extremely large files then it shouldn't make much of a difference.
I would agree that this is probably not something that you should concentrate on too much. Most of the time you can use includes freely without worrying about how fast it is. Ultimately, includes like this are for saving your time as a programmer.
Another factor to think about on the very low level is that the file you are accessing might be at a different place on the disk. So, that means that it may not be cached, and it will take longer to go to the file location than if it were located within the file itself.
Related
First let me apologize if this question was answered before, I just don't seem to find the proper terms to search for it.
The context
I'm viewing some complex PHP code with a lot of require in it, it is kind of ineffective and time consuming to search for a function's or variable's definition through all the multiple level tree of files included in the current file.
The desired result :
so I'm wondering if a function exists that does the following:
Remove all those requires
Replace them with their code recursively
Output it on the browser or return it to a variable
In a nutshell, a function that prints the final PHP code to be executed.
The question :
Is there such a function? if yes, what is its name?
Thank you for the help.
You cannot get the code which is going to be executed because inclusion of code can be done at run-time, based on variables whose values you don't know before running the code.
What you can do is archive all the possible code to be executed, and pack it up as a phar archive.
Using modern tools, that could mean using composer archive.
More information available here https://getcomposer.org/doc/03-cli.md#archive
Besides, it would go against the ecosystem: in modern PHP applications, you use autoloading. Which means that even in projects with millions of lines of code and hundred of thousands of files, there is only one include/require statement.
This question already has answers here:
Is there any way to "auto-use" certain modules everytime I write a script?
(2 answers)
How can I export a list of modules with my own module?
(1 answer)
Closed 7 years ago.
In a PHP include, code is parsed as if it was written right into the source file. You get the same result as if you literally copy and pasted file A into file B.
I'm looking for a way to do this in Perl. The number of libraries I have to 'use' in every script is getting annoyingly large, and especially annoying is having to use v5.14 just so I can have the say function. I'm actually looking up how to alter the interpreter to always use the latest version of Perl at this moment, compatibility with the rest of the world be damned, but this won't solve the rest of my list of includes.
Edit: none of the linked answers answer the question.
The equivalent of PHP's include is do EXPR. There is also require EXPR, which is like require_once, and use, which will also call import on the package.
However that is probably not what you want. If you have a lot of .pl scripts without packages, you are dealing with legacy code. You need to be carefull what you require and include where.
I actually ran into a problem today. I have got a site where multiple pages have the same layout. Today I had to change a thing on this layout and had to change every single page. This made me think about solutions for this.
So, my question:
Would the performance and loading speed of the website be affected if I had the layout HTML in one single file and read it using PHP on every page? This would make it a lot easier for me, since I only had to change the layout in that single file.
Also, how should I read this file. At the moment, I would use the following code:
for ($count=0; $count < $dirAmount; $count++) {
$path = " //path\\ ";
$fileText = split("--", file_get_contents($path));
echo " //code\\ "
unset($fileText);
}
But would there be a faster way?
BTW, if this is too trivial, please note. I couldn't find much other information that actually was clear enough to help.
Simply use php's include or require to include the file.
It will affect performance a little bit, but that tiny little performance cost is insignificant compared to the ease of having one file you can use in multiple places.
One suggestion would be to use full file paths on include/require statements. Normalizing a relative file path can be expensive; giving PHP the absolute path (or even “./file.inc”) avoids the extra step. - Source
I would suggest you consider using smarty or twig if you want to start using templates for your website. Those engines optimize page loads by pre-caching pages and optimizing them to load as quickly as possible.
As for your original question, no, especially if the file was called repeatedly by the entire website, it would most likely get cached into RAM and load pretty fast since it was being called often. As Cerbus mentioned, using include or require would probably be the fastest way to process the files.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If I'm hosting a website on a server that has PHP, would I ever need to host a .html file on it? PHP files can have raw HTML code inside of them as well as the added benefit of being able to inject PHP code, so isn't PHP a more inclusive language?
Serving HTML is generally a lot easier for the webserver than serving a page that has to be run through the PHP interpreter.
This can have huge benefits on high-traffic sites. For example, the Obama campaign ran their fundraising platform with a lot of static HTML for that reason.
it always can be situation that you would need to add e.g. maintenance page in pure html
It's not a question of whether you can simply make everything "a PHP file" but rather a question of whether any given file needs to be PHP. If a file contains only static content, there's no need for any server-side processing. So in such cases it would make more sense to make it an HTML file.
Don't look for a tool that covers all cases, look for the right tool for each case.
I read that it is recommended to have <?php flush(); ?> between </head> and <body>. I would assume that this should apply to every webpage, there really isn't a solid reason as to why you would only use HTML.
I agree with ceejayoz, but if that's not a problem for you, then using PHP is great.
It depends on your purpose. If your page uses PHP then the file extension needs to end in .php. If you don't have any PHP then you can just save the page with a .html file extension. A major difference is that PHP is processed on the server side. Meaning the user(client) will not see your PHP code if he views the source, he'll see what was processed from the server in the form of HTML.
One advantage of using .php files over HTML (in this trivial case) is that, you can wrap up all your footer files(if the are the same) or any redundant files, like sidebar, advertisingand just include them in your other files, instead of having to create/manage individual files later, So, you can just open one footer.php file and make as many changes as you want without wasting any time.
Your basic understanding is correct. You can absolutely have a .php file that has only HTML in it and given a simple page and a simple site there would be little difference.
However, the PHP preprocessor adds a bit of overhead to check for PHP code in the file. In most cases this is insignificant, but on a highly optimized site you probably would not want the pages to be processed for nothing.
I wouldn't say that PHP is a more inclusive language though, HTML and PHP are two different things. In many cases (but by no means all) PHP generates HTML. It's just that the resulting HTML from a PHP script that has no PHP tags in it would most likely be the same as it would be for an html file. Although it is likely that HTTP Response Headers would be different and there are other things outside of the file content itself that could be slightlu
Continuing this question What is the best way to manage duplicate code in static HTML websites ...
I'm starting to create PHP variables for each chunk of duplicated HTML code. My website is divided into themes and each theme has its duplicated code. I have the option to:
save all variables into a single PHP file, and then include it at the beginning of each HTML page (even if the page only uses one or two of those variables);
create a PHP file for each "theme" and include at least two PHP files at each HTML (one with the common variables and other with the specific variables.
Which one is faster? Including several small PHP files increase the page's loading time? I think the "non-monolithic" version is easier to maintain... I just need to know if I'm sacrificing performance.
I just need to know if I'm sacrificing performance.
Except that you don't. This is premature optimization. Go for the one that maximizes maintainability and ease-of-programming.
I'd go with one file including all the necessary files or themes, that makes maintenance a lot simpler. If you mean faster to develop, that's also the one to go for.
I wouldn't worry about the performance. Your site is never going to be as slow including files you don't use as some of the frameworks out there.