Length of PHP

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Length of PHP

Post by Epiales »

I was just sitting here thinking about what I need to do next on my game (so much to do), and a question came to mind.

Does it matter the length of a php file? I mean, if it's VERY VERY long, does it make a difference? If so, what would it do?

I ask because I am using nothing but bypasses on one page to do everything in the game basically. So this one page is going to have a huge amount of information on it.

Thanks for the information and help guys

EPIALES
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Sim
Posts: 410
Joined: Sat Dec 26, 2009 5:37 pm

Re: Length of PHP

Post by Sim »

Well how long is long? Are we talking a few thousand lines? or a few hundred thousand lines? or MORE?


What kind of information are you talking about? Are you talking about information that should be stored in a database? If so, it should be stored in a database or maybe txt files?


If possible, could the file be broken into multiple files for easier editing?
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Length of PHP

Post by Epiales »

Sim wrote:Well how long is long? Are we talking a few thousand lines? or a few hundred thousand lines? or MORE?


What kind of information are you talking about? Are you talking about information that should be stored in a database? If so, it should be stored in a database or maybe txt files?


If possible, could the file be broken into multiple files for easier editing?
It's basically storing mysqli code to put into database or recall from database, as well as the actions that are performed upon clicking links. I don't know how long it WILL be, but it will be a very big file.

Basically I have a left side php that stores links and a right side php that stores links. I have them php included into the main page. Then in the center page I have it blank where it displays the actions when you click a link on the right or the left sides. On the middle part, it will include tons of mysqli formula's and information.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Length of PHP

Post by KyleMassacre »

I don't think it would make much difference if it's some sort of brain file. The only issue I would see is if it was like a continuous script that just never ended where you just store a grip of global PHP variables, then for your server to parse it and execute all the needless queries you may run into some lag. But if you just execute what you would need when you need it, it shouldn't be an issue.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Length of PHP

Post by Jackolantern »

OK, this is a bit of a complicated question concerning performance. I have to rephrase your question a bit to be able to break it down: "Should I break a large file down into smaller files and include() them together?" The answer to that is no, you shouldn't, if the only concern is the file length.

To explain I will briefly describe what occurs in the PHP engine when a request comes in. The system determines what file(s) are needed to fulfill a request. Then the main file is read into memory, and then all files include()'d in the main file are read into memory. Then the engine parses the code and turns it into executable instructions (a lot of stuff goes on here, but it isn't important for this question). So if you have a file that is 10,000 lines of code and break it down into 5 files with 2,000 lines each, the Zend engine still has to load 10,000 lines of code into memory before anything will run. The difference is you have now piled 5 file openings onto the load time instead of just 1. Nothing was saved.

Of course, you should break files down to help you stay organized. In that case you should use classes and make smaller files in whatever way helps to keep your code neat and easy to work with.

The second part of the issue is "How long is too long?" and that is tougher to answer. If you need the logic, you need it, so there isn't a ton that can be done, but there could be some. There is a distinction between file loading, which is what I was talking about above, and execution. When loading, the script is read into memory like a text file and then processed to prepare for execution. There is no difference what is in the file when loading, be it class definitions, procedural code, etc. A character is a character when loading. But when executing, a line is not a line and a character is not a character. Of course, only a line that is being executed is taking up any time during execution. So if your file has a ton of SWITCH or IF statements that are skipping a lot of code depending on logical decisions, then a large file with many lines probably won't matter for execution time because each time the file is run, only a portion of its code is actually executing. The main concern is how many instructions are actually being executed on each execution of the script.

I will say however that on a modern server, about 90% of total page server-time is spent loading and parsing scripts versus actual execution. If you have ever heard of "PHP compiler" applications, such as the one offered by the Zend company, what they are doing is pre-loading all of the PHP scripts into memory and forming the executable memory objects to cut way down on that loading time. They can be quite critical for high-traffic websites.

So I hope that answers the question. It is actually a pretty complex question to answer, but let me know if I wasn't clear on anything 8-)
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Length of PHP

Post by Epiales »

Jackolantern wrote: So if your file has a ton of SWITCH or IF statements that are skipping a lot of code depending on logical decisions, then a large file with many lines probably won't matter for execution time because each time the file is run, only a portion of its code is actually executing.
This sounds about right... but my conern was, let's say you have 100K users just clicking away. That was my conern LMAO! Not that I'll have that many, but hell, why not dream? lmao!

So one person might be clicking on the robbing section while 50k are clicking on the breaking in a house section, 30 k clicking on this and that section and so forth. Each section run off one php page using bypass to do so.

Thank you for the detailed information.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Length of PHP

Post by Jackolantern »

No problem! :)

If concurrency (as in, many people on the server at the same time) is a problem, I would look into a PHP compiler like I mentioned before. It can really speed up the time to load pages provided you have the memory to handle it (and pretty much any web server today would).
The indelible lord of tl;dr
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: Length of PHP

Post by a_bertrand »

Actually, it depends if you need all the code all the time. If you need it all the time, then splitting will actually maybe slow down. If not then it could well be that it will speed up. For example: if you need a piece of code only once in a while, put the include after an if, and you will not load it all the time.

Sadly code length, and number of files to load in PHP do matter where in Java, C#, C++ or even Python it doesn't matter so much.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Length of PHP

Post by Epiales »

a_bertrand wrote:For example: if you need a piece of code only once in a while, put the include after an if, and you will not load it all the time.
Great Idea! I didn't know u could do that. Thx :)

EPI
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Length of PHP

Post by Jackolantern »

a_bertrand wrote:Actually, it depends if you need all the code all the time. If you need it all the time, then splitting will actually maybe slow down. If not then it could well be that it will speed up. For example: if you need a piece of code only once in a while, put the include after an if, and you will not load it all the time.
Ahh, I actually did some double-checking on this and this is something that was changed in the fairly recent past in PHP. You are right, an include inside an IF statement that is not executed is not loaded. PHP used to do a sweep through the entire main file and would preload everything linked in the file.
The indelible lord of tl;dr
Post Reply

Return to “General Development”