How Many PHP Includes are Too Much?

Shelldon

Active Member
Joined
May 30, 2012
Messages
53
Hi all. As a PHP newby I have a question.

I've created a index.php page which has the follow..

header div
nav div
slide div
latest news div
newest members div
advert div
footer div

The index page code now stands at 500 lines, and I'll probably hit 800 lines since I've still got the footer to do. So my questions is, can I create a header.php, nav.php, slide.php and so on and then include them onto the index page?

It would make my code far easier to read (for me anyway) and when I get a php developer to go through it all, I'm thinking it would be easier for them as well.

That would mean having 7 includes. Would this be ok?

Thanks
 
Last edited:

rward

Senior Member
Joined
Oct 26, 2007
Messages
865
over 9000


7 includes will not affect anything.

If you really want you can benchmark it and find out but I get the feeling it's going to take an extra 0.00002 seconds of CPU time.
 
Last edited:

koeksGHT

Dealer
Joined
Aug 5, 2011
Messages
11,857
Well my index page is

<?php include 'inc/core.php'; ?>
<?php include 'inc/secure.php'; ?>

<?php include 'inc/header.php'; ?>
<?php include 'inc/nav.php'; ?>
<?php include 'inc/footer.php'; ?>
 

craiglotter

Well-Known Member
Joined
Jul 11, 2014
Messages
263
Hi all. As a PHP newby I have a question.

I've created a index.php page which has the follow..

header div
nav div
slide div
latest news div
newest members div
advert div
footer div

The index page code now stands at 500 lines, and I'll probably hit 800 lines since I've still got the footer to do. So my questions is, can I create a header.php, nav.php, slide.php and so on and then include them onto the index page?

It would make my code far easier to read (for me anyway) and when I get a php developer to go through it all, I'm thinking it would be easier for them as well.

That would mean having 7 includes. Would this be ok?

Thanks

This is more than fine. There isn't really a hard limit to the number of includes that you apply, and the performance impact is negligible in real terms.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Hi all. As a PHP newby I have a question.

I've created a index.php page which has the follow..

header div
nav div
slide div
latest news div
newest members div
advert div
footer div

The index page code now stands at 500 lines, and I'll probably hit 800 lines since I've still got the footer to do. So my questions is, can I create a header.php, nav.php, slide.php and so on and then include them onto the index page?

It would make my code far easier to read (for me anyway) and when I get a php developer to go through it all, I'm thinking it would be easier for them as well.

That would mean having 7 includes. Would this be ok?

Thanks

http://mybroadband.co.za/vb/showthread.php/827571-PHP-vs-CSS
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
Eventually you want to use common layouts, and only extend parts of them.

I say just use something like twig. Super simple to add to any project, stops you from opening db connections in your templates, and zero direct php includes and echoes :)
 

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
Eventually you want to use common layouts, and only extend parts of them.

I say just use something like twig. Super simple to add to any project, stops you from opening db connections in your templates, and zero direct php includes and echoes :)

Was going to suggest something similar.

For ****s'n'giggles I wrote my own template system (really lightweight/no major features) in PHP and my index page is maybe 3 lines of code calling it. The rest is loaded dynamically (classes) and the content output by whatever SEO friendly URL you're visiting (if it can find it in the DB, almost like wordpress does)

Many things you can do, my first version totally killed CPU when more than 100 requests occurred per second.

7 includes with perfectly fine code won't kill anything :p
 

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
[)roi(];18203933 said:
Any overhead is easily optimized away by employing bytecode and JIT; Facebook has a actively developed open-source project called HVVM that caters to this.

HHVM sucks and is still in infancy. Most distros have it as install now through yum or apt-get, and you will see a speed boost, but some cavaets like it not clearing sessions out can quickly kill your innode usage and make your server go bye-bye, it also doesn't have the ability to split pool usage in it's own groups like php-fpm can to make sure that one website can't access another website's files with the case of shared hosting (or hosting multiple domains on one VPS)

PHP7 is nearly as fast (and faster in other instances) as HHVM, and unless you are planning to embrace the entire hack language and convert everything, I'd stick with a PHP processor until they can refine / grow HHVM more. Facebook wrote it for themselves, and it works well for them, unfortunately it's not always the case in the real-world
 

flippakitten

Expert Member
Joined
Aug 5, 2015
Messages
2,486
I always say, if you want to see how much you can include, have a look at any MVC framework...
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
HHVM sucks and is still in infancy. Most distros have it as install now through yum or apt-get, and you will see a speed boost, but some cavaets like it not clearing sessions out can quickly kill your innode usage and make your server go bye-bye, it also doesn't have the ability to split pool usage in it's own groups like php-fpm can to make sure that one website can't access another website's files with the case of shared hosting (or hosting multiple domains on one VPS)

PHP7 is nearly as fast (and faster in other instances) as HHVM, and unless you are planning to embrace the entire hack language and convert everything, I'd stick with a PHP processor until they can refine / grow HHVM more. Facebook wrote it for themselves, and it works well for them, unfortunately it's not always the case in the real-world
Like anything; if you don't know what you're doing the result is going to suck. Being aware of the caveats is always a good starting point; as FB says on their homepage "We are aware of minor incompatibilities..."

Also remember speed is relative.
1*JV2bk-3kOXr2-yU7FJd91w.jpg
 
Last edited:

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
[)roi(];18206429 said:

I understand. I'm running HHVM on Debian for my Wordpress installs. It's fast, easy to configure, no hassles and certainly a 1 up on PHP5.6 with regards to memory usage and CPU. However as I said, several months into it you get your server going down for no apparent reason (looks like a disk space issue) and eventually figure out HHVM used up all your innodes due to an issue that was reported almost 2 years ago (which is still even in their nightly build)

If I were to do custom coding for sites again, and choose hack, I'd use HHVM (if I plan on only hosting 1 site on the server). If PHP, I'd stick with PHP7 FPM etc. Maybe even do the NodeJS route. For now I'm just concerned about keeping my server up without having to worry about a faulty engine being the cause of it instead of a ddos or corruption.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I understand. I'm running HHVM on Debian for my Wordpress installs. It's fast, easy to configure, no hassles and certainly a 1 up on PHP5.6 with regards to memory usage and CPU. However as I said, several months into it you get your server going down for no apparent reason (looks like a disk space issue) and eventually figure out HHVM used up all your innodes due to an issue that was reported almost 2 years ago (which is still even in their nightly build)
Yeah that's always going to be an issue with Linux FS, but in a way it's probably better than what happens on NTFS i.e. too many files can easily cause a lock up; even experienced a few that wouldn't boot.

As for your issue, the only one that I could find is this one (Dec 2015):
...and the only community recourse I could find is this: http://community.rtcamp.com/t/hhvm-session-files/3639

As for support in general from FB; in my experience they generally quite helpful; granted I don't interact with the HHVM guys; but I'm sure you could easily find a few on Twitter.
If I were to do custom coding for sites again, and choose hack, I'd use HHVM (if I plan on only hosting 1 site on the server). If PHP, I'd stick with PHP7 FPM etc. Maybe even do the NodeJS route. For now I'm just concerned about keeping my server up without having to worry about a faulty engine being the cause of it instead of a ddos or corruption.
Didn't know about Hack; but in looking over the language it looks still a little undone TBH. Why not consider something like Go, Rust, Ruby, Swift, ...
 
Last edited:

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
[)roi(];18213067 said:
Didn't know about Hack; but in looking over the language it looks still a little undone TBH. Why not consider something like Go, Rust, Ruby, Swift, ...

I used it as an example. Facebook wrote HHVM and their language Hack to be compatible/spaghetti with PHP to (with great success) implement less servers for the load they're seeing.

My point there was, if I'm going to use Hack, I'll use HHVM. If I'm going to PHP, I'll use PHP7 FPM etc.

I won't run PHP on a Windows Server for example. So if I use Go I'll use whatever is best for Go.
 
Top