I used php on my website so it can display multiple languages. I noticed the size of the html files reduced because most of the text was moved to a new large php file, which contains all "idiomatic" text in all languages available (just two in my case).
My problem is: Previously a html page was downloaded with just the necessary text. Now a smaller html page is downloaded but a large php file is always included, whether the html uses more or less lines of it.
Questions:
- When a php file is included (
include_once(file.php);
orrequire_once(file.php);
) what is happening? Is the content of thefile.php
being copied to the html? Is it just used to say to the language processor "If you need to resolve a name, you may want to look atfile.php
"? - What is more (speed) efficient; to have one large php with every language of every html page inside; or to have one php file per html page only with the code needed by that page?
- Should a php be 开发者_JS百科included right before being used, or should be a general include at the top of the page that gets most of the php that will eventually be used along the html page?
- Finally, does php increase a page loading time?
P.S: Anyone tried PHP Speedy? Does it really works? Does it have any problems (compatibility) ?
I found out:
- (check solution)
- Both solutions have exactly the same performance. I chose to separate the file by language because it feels tidier.
- Same as above, no impact on performance.
1) Every include / require statement is replaced by the content of the included / required file. The content of the included file itself is becoming part of the executing script however if it will become part of the HTML response depends upon the content itself.
2) Unless you have a very large number of visitors you will hardly notice a difference between including one large file and several smaller ones. However its good practice to include only parts which are actually required because that way you are saving web server memory and resources which might present a problem in case of a large number of requests.
3) It doesn't really matter where you include your code. For pure organization reasons its good to include all the stuff right at the beginning of the script however sometimes this is not possible which means that you'll often see code included inside functions etc.
4) There is some latency involved with parsing PHP files when compared against pure HTML files because the process involves a "roundtrip" to the web server extension itself however because of the internal caching mechanisms etc. that latency is very hard to spot, if not almost impossible to prove statistically.
EDIT: If you are concerned about your web site performance a good place to start are some of the Google Chrome extensions such as Page Speed and Speed Tracer. They will help you pinpoint the usual problems that might lower your web site performance.
For the server side its always good to use code profilers to find potential performance hogs such as high number of function calls etc. For that you can use xDebug PHP debugger which comes with an excellent profiler and WebGrind to analyze the performance itself.
Previously a html page was downloaded with just the necessary text. Now a smaller html page is downloaded but a large php file is always included, whether the html uses more or less lines of it.
I don't like this approach. Why load unnecessary stuff? Plus as your site expands, it can become a very bloated file.
There are other ways, like:
1) Fetching text from database.
2) Storing text in separate files depending on language. This one you can easily do even if you're new to php. And it should be faster than your current solution.
On your questions:
When a php file is included (include_once(file.php); or
require_once(file.php);) what is happening? Is the content of the file.php being copied to the html? Is it just used to say to the language processor "If you need to resolve a name, you may want to look at file.php"?
When you include or require a file (and if it successfully loads), server will execute whatever code you have in it.
It's a very broad question for more detailed answer, check out some literature.
Should a php be included right before being used, or should be a
general include at the top of the page that gets most of the php that will eventually be used along the html page?
Both approaches are valid and appropriate for different situations. Sometimes you need some code executed, and hence included, before anything else happens (in your case it might be recognition of which language should be used). Other times you might wanna include footer, so it only makes sense including it somewhere on the bottom.
Finally, does php increase a page loading time?
Depends on situation, you'd have to measure yourself. But it's no reason to stay away from php.
It all depends. In order,
- No. When you say
include
, PHP does EVERYTHING infile.php
and then continues with the one that hadinclude
in it. - It depends. I think that the 1 page/language setup would be a little bit slower.
- There's no difference. I usually put them at the top, because everything is in the same place.
- It might, but I don't think so.
And no, PHP Speedy is responsible for minifying and merging your JavaScript and CSS. Minifying means this:
div#content {
color: #000;
}
becomes this:
div#content{color:#000;}
Yes, when you use PHP it takes longer for requests, but smaller requests don't take much time at all. the difference isn't really noticeable from PHP to HTML.
When you include something, PHP is parsing it as if it were a php file (and that requires loading the file from the HDD for every include).
精彩评论