PHP vs CSS

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Right, in true mybb fashion my heading is as misleading as it gets. Lets continue.

You know how you should always use a single CSS file instead of six ( that's why Sass is such a appealing thing to get into get sassy! ) Since if you have stylesheet1, stylesheet2, stylesheet3, stylesheet4, stylesheet5, stylesheet6 then you will have six trips to the server slows everything down etc etc right?

Now, I use php like so:

index.php

inside I use includes example:

footer.php
sidebar.php
header/menu.php

Now my question is, does this includes have the same effect as the CSS or is this not the case due to php living on the server and css living in the browser?
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
@gkm

PHP:
(index.php)
for ($i=0; $i<100000; $i++) {
    include('somefile.php');
}

(somefile.php)
<?php
// nothing here

Takes me 15 seconds to run

PHP:
(index.php)
for ($i=0; $i<100000; $i++) {
    echo $i .'<br/>';
}

Takes me 0.12

2016-07-06_9-05-23.jpg

2016-07-06_9-05-44.jpg
 

Praemon

Expert Member
Joined
Jan 11, 2007
Messages
1,678
@gkm

PHP:
(index.php)
for ($i=0; $i<100000; $i++) {
    include('somefile.php');
}

(somefile.php)
<?php
// nothing here

Takes me 15 seconds to run

PHP:
(index.php)
for ($i=0; $i<100000; $i++) {
    echo $i .'<br/>';
}

Takes me 0.12

View attachment 373751

View attachment 373753

Realistically no code base is going to have 100,000 files (and obviously you'd be using include_once), but I get it you're just using it as an example. But either way, you can't see it the same way as CSS files. The cost for multiple CSS files is multiple HTTP requests, whereas for PHP you have to look at it differently. The performance cost in PHP depends on what's in the included file and how much of it is being used. If you have one large PHP file included, but only use 1 function, the cost could be seen are high. Where as having 5 small files with a few but mostly used functions, would in theory deliver a lower cost on performance.

If you're worried about including too many files, look at using autoloaders so that the file is only loaded when the class is called. Having a few hundred files isn't going to be detrimental to the project if loaded correctly.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Realistically no code base is going to have 100,000 files (and obviously you'd be using include_once), but I get it you're just using it as an example. But either way, you can't see it the same way as CSS files. The cost for multiple CSS files is multiple HTTP requests, whereas for PHP you have to look at it differently. The performance cost in PHP depends on what's in the included file and how much of it is being used. If you have one large PHP file included, but only use 1 function, the cost could be seen are high. Where as having 5 small files with a few but mostly used functions, would in theory deliver a lower cost on performance.

If you're worried about including too many files, look at using autoloaders so that the file is only loaded when the class is called. Having a few hundred files isn't going to be detrimental to the project if loaded correctly.

Sweet perfect! ( Yes totally agree my example was more a theoretical one an not practical. )

Okey so then I should be fine.

My inlucdes are

footer.php
header.php
sidebar.php
etc

Inside in just html I do it to keep my amateur code clean and "modular" in my own way.
 

Kosmik

Honorary Master
Joined
Sep 21, 2007
Messages
25,665
Think of it this way: PHP=server load vs CSS=Client load. Regardless of what you code on server side: php, c# , vb , whatever, the client receives HTML and CSS.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I completely understand now. In essence if I understand this correctly the only time an include would hold up the load time is if that include had some function in that is quite complex something like images processing or the likes which needs to take place on the server first for instance?

In other words me splitting my html up into php includes should be negligible right?
 

Kosmik

Honorary Master
Joined
Sep 21, 2007
Messages
25,665
I completely understand now. In essence if I understand this correctly the only time an include would hold up the load time is if that include had some function in that is quite complex something like images processing or the likes which needs to take place on the server first for instance?

In other words me splitting my html up into php includes should be negligible right?

From a load perspective yes, from a code manageability, if done properly it's much better, especially if you are re-using sections.

If you ever want to see what a page does in the background network wise, install firebug and firefox. Great analytical tool to help you see what traffic is flowing between your webserver and a browser.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
From a load perspective yes, from a code manageability, if done properly it's much better, especially if you are re-using sections.

If you ever want to see what a page does in the background network wise, install firebug and firefox. Great analytical tool to help you see what traffic is flowing between your webserver and a browser.

I love you
 

giggity

Expert Member
Joined
Feb 19, 2011
Messages
1,024
... PHP=server load vs CSS=Client load ...

Not entirely. If you store the CSS files on the server, that's still a request per file ;)
...Assuming you aren't directly injecting the CSS into the PHP using <style> tags. :sick:
 

francdore

Banned
Joined
Feb 16, 2011
Messages
285
PHP is read by the server to spit out HTML to your browser.

CSS is a file(s) included in your HTML to style the HTML.

You have to keep/store the CSS on the server but to make it use as little resources as possible you can compile it into one file and then minify it to make it load as fast as possible.

Not sure if that was what you wanted to know?
 

flippakitten

Expert Member
Joined
Aug 5, 2015
Messages
2,486
Right, in true mybb fashion my heading is as misleading as it gets. Lets continue.

Lol!

If you want to get an idea of how disconnected you can get, have a look at any MVC framework, Yii is one of them (http://www.yiiframework.com/)

So you might get some slowness if you have millions of requests but then you can just throw hardware at the problem.
 
Top