After i made some sites now , my programing style evolved to something very strange.
so i have to points for which i wonder if thay are slowing down the code execution:
1st - i'm breaking my code to too much , small , reusable (and i do re-use them) files, so do the many include() and require() (is the overhead of accesing an other file significant) slow down the pusing very mach? (is the overhead of accesing an other file significant)
and 2nd - I use a lot of lines like this
some HTML <?php echo $lang['login']; ?> some HTML
so here i just add 1 word to the HTML with a whole PHP block-- is there overhead for constantly starting and ending a PHP blocks
And I will be happy to hear how ur organizing ur PHP Programs (sites,code) too.
If there is a difference, in both cases the result will be faster and not slower.
1. There are 2 levels of caching when the execution of PHP is concerned:
- The webserver and operating system will cache all frequently used files in memory. This means that if your PHP script is executed more often, all of the files it includes will be cached in memory. In that case there will be no overhead when reading them. If they have to come from disk on the other hand there will be some overhead (but it will be very small).
- When PHP executes a script it first compiles it to a so called Intermediate Representation (or bytecode). This is a kind of 'image' which contains all information the PHP interpreter needs to execute your script. This 'image' can be executed by PHP without having to parse your script: it is "ready to run". Most implementations of PHP have an accelerator. One of the things this accelerator does to make things faster is to save compiled IR's of executed PHP scripts (the image), preferably in memory, or else on disk. When your script needs to be executed, the accelerator first checks to see if it already has an ready-to-run compiled version of your script. If it has, it checks to see if the source of your script has changed since compilation. If not, it runs the image, and doesn't read, check, parse and compile the entire program again.
This means that by cutting your scripts into little pieces you give the accelerator more opportunities to keep parts of your script in compiled form: when you change something, only the script in which you make changes has to be recompiled, so the more you divide your code between seperate files, the less has to be recompiled. Since the compilation-phase of executing a PHP program takes the most time and running the compiled IR can be done very quickly, it is an increase in efficiency when there is only a little script which has to be recompiled.
2. When PHP parses the HTML file it looks for php tags. As long as the parser doesn't encouter this tags it keeps on passing all HTML to the server without changing it or looking at it. When an opening tag is encoutered, PHP begins parsing everything between <?php and ?>. This is extra work. Even worse: since you have to do all output between <?php and ?> using strings (for example echo "<BR>"), PHP has to process all these strings. It checks them for variables to be interpolated and for escape sequences. After that it builds an instance of the string in memory before passing it to the server. When YOU know the string will only contain raw HTML, but you put it in a string between PHP-tags, PHP does not know it contains only raw HTML: it has to check and process it anyway.
So by keeping the parts of your code between PHP tags as small as possible, you minimize the work PHP has to do. It only has to look for PHP-tags, and only when it finds one the interpreter kicks in and starts to do the heavy work.
Also, without looking at efficiency, your coding-style isn't strange. Actually both points are examples of good coding-style. The first increases code-reuse which is always a good thing (saves time, debugging, testing, etc.). The second makes your HTML/PHP files much more readable compared to scripts where all HTML is outputted using echo-statements.
I have never seen such a good answer to a question
10x a lot
" it is an increase in efficiency when there is only a little script which has to be recompiled." -- i think this is not big of a deal, because once u made the site u don't change it every day , but it's nice to know this
"The second makes your HTML/PHP files much more readable compared to scripts where all HTML is outputted using echo-statements." -- that's why i do it like this
and one more related question --- I use this approche for storing words(strings) for the difrent languages (depending on the languge i include the appropriate file). What's your way of doing it? Should i store them in SQL Database or something.
$lang['register']=' Register ';
$lang['forgoten_pass']='Forgoten your password?';
Just like to add, for your second question about php blocks within your html. You may consider using smarty (smarty.php.net) for this as I feel it is more readable, but I don't know if it is worth switching to.
Also, I have read that including is a slow process, but if MrBlueSky is correct about the caching, then the slight overhead would not be a problem.
As for your latest question, I have used that style ($lang['something]) because I copied some styles from phpBB2 a while back. I don't know how ever if it is the best way. I would not use a database though, that only seems to make sense if you really need users to be able to edit it through some form. This kind of data is pretty static so you don't need something as dynamic as a database (you probably won't change things too often once your site is running).
i too use phpBB2 for a reference from time to time. And this $lang['sh'] thing is from there
and for the smarty.php.net --- i'll check it more in dept when i have more free time , sound interesting
i may have the chance to work for one web dev firm and they are using http://codeigniter.com/. It very good i got the basics very very quick. Great framework.