Jump to content

Newbie: Coding best practices, & for speed?


son0fhobs

Recommended Posts

Greetings all!  New to this forum, but want to say I've learned a lot already!

 

My experiencing in php and programming in general is minimal, so I do appreciate you humoring me.  I guess the question comes down to, how is Php parsed?  Primarily the context for this question is optimized speed.  I heard that it goes through all comments included, hence being verbose in your final copy may not be ideal (for speed, not practicality and maintenance).  Do I program with more or less functions, separate files, more or less loops, etc. 

 

Now for my specifics:

1. The specific situation is that I'm building a plugin for Wordpress, but php in general.  I want to make it flexible, but I don't want to bloat the code in the process.  Lets say the user will choose the style, and it be set permanently.  If put in the same CSS file, all the extra will always be read, so what if I made two different stylesheets.  If I made an if/else include, would the php ignore the include of the file not chosen, hence not slowing things down?  Or is the only way for the code not to be read by literally having the user change the filename of the css?  (css is just the convenient example, but large php functions have a similar situation). 

 

a. Do all unused functions get read anyway?  (my experience is yes).  All loops get fully processed even if not used as well, right?

 

b. I'm using someone else's code, and two functions are almost identical.  I'm guessing it's bad to nest functions (is it?  For speed or usability/maintainability?).  Should I create an extra variable that helps tell the function which extra bit of code to process? 

 

This is more of a general concept, I just don't know how to search for it, and haven't found it.  Not having that core foundation, I'm guessing a lot of it would have been answered along the way.  So if you can point me to resources as well, that would be amazing!

 

Once again, thank you all!  The more I get involved, the more I aboslutely love the programming/open source community!  My how things have changed from 10 years ago!  Love it!

Link to comment
Share on other sites

PHP as you know is an interpreted language and has been around a long time, not only that it grew up in an environment where speed is important.  PHP parses files quickly, be as verbose as you want with your code and comment the hell out of it.  While in theory the things you mentioned may impact how quickly a file is interpreted the practical result is insignificant.  Nearly all production servers have a PHP cache of some kind run from a opcode state throwing arguments of parsing time out the window.

 

I think you may be under the assumption that parsing and interpreting are the same, but the truth is parsing is the reading of a thing and interpreting is the realization.  It is true that the PHP parser will read all of your code, but this is mostly just a conformance test of the PHP language, when it comes to interpreting the code it will only execute the things based on your code flow.  Say you have and if/then/else, if the condition is met, do a loop, if the condition is not met, do a different loop.  If the condition is not met, the code meant for the true condition is never run.  So when you ask if unused functions get read, well they get parsed, but if they are never run in code they are never executed.

 

include()'ing files is a slightly dangerous thing if you're not wary and in your case if I understand it is not what you need.  Using include() is to slurp up a file and have it inserted at that point in your code as it were more PHP.  The better approach is to point the browser towards the file, simply echo or print a link to it and let the browser do the work.  Under a caching solution this may be different, but for the most part letting the browser get the file is fine.

 

A nested function is function within a function, not a similar function.  As for similar functions convention says we should try mitigate duplicate code as much as possible, but if there is legitimate reason, like an improved optimization or external dependency go with the added function, just make sure to document it its use case.

 

You seem to be overly concerned with performance for someone admittedly new to PHP programming.  While not a bad thing, try to focus it a bit on things you can change, that is your code and as such the tools to help you do so.  xdebug is a great PHP profiler, google it down and set it up on an install and you'll quickly develop an understanding of where bottlenecks lie in your own code and how to better adapt to them.

 

If you want to learn more about PHP first off code and dive into the online manual, it has lots and lots of comments and examples and opinions.  Its really one of the better language resources. 

Link to comment
Share on other sites

Holy crap dude, that was the most amazing and thorough reply ever, thank you!  So, so, so incredibly helpful!

 

You're right, understanding parsing/interpretation makes all the sense in the world and explains a lot.  I've been thinking about it, thanks for the motivation to get on xDebug. 

 

Once again, thank you so much for your thorough, understandable, and effective reply.  (not to mention prompt!)  Man, I love the "geek" programmer's community, open source, and such.  Appreciate the help

 

Cheers!

David

Link to comment
Share on other sites

Do NOT code for "speed". Code for manageability. For speed, install an opcode cache. The result of parsing PHP code by the Zend engine are "opcodes", instructions for the Zend engine which is written in C. Caching these instructions will dramatically reduce response times, especially when using large libraries.

 

If even that is not enough, which is highly unlikely, there's HipHop for PHP, which basically transforms PHP to C++ which you can compile. Google it.

Link to comment
Share on other sites

Oh awesome, I'll check it out.

 

Specifically, I'm using wordpress, which can be incredibly slow at times.  Hence my pursuit for speed.

 

Aren't opcodes applied to the server?  I'm not hosting my site, so that's why I never pursued it, but I may well be mistaken.

 

Of course once I get all this online I'll get caching and other techniques to speed up the site, I just wanted to make sure I was coding effectively/efficiently from the beginning.  But I guess that's where benchmarks come in.  And I'll post it online later for some critique.  Once again thank you so much!

Cheers!

Link to comment
Share on other sites

I just want to say the original replay to this thread was great! Very thorough and well thought out reply. Also on the basis of HipHop, HipHop is only effective in a high bandwidth, multi-server environment.  You will actually lose performance in a single server install.

Link to comment
Share on other sites

HipHop is only effective in a high bandwidth, multi-server environment.  You will actually lose performance in a single server install.

 

Bullcrap. The same code will run faster compiled using HipHop for PHP than using the Zend Engine, even with an opcode cache. This has nothing to do with the amount of servers and certainly not with the amount of bandwidth available, that is ludicrous misinformation. That's like saying a faster engine in your car will make your car slower on a narrow road.

 

That said, it brings it's own suite of issues and should never be your first option (I think I've made that clear before). But if straight processing power becomes an issue (so after you've eliminated parsing time, optimized database access, and exploited caching to it's practical maximum), whether on a single server or on a cluster, HipHop for PHP *might* allow you to go a step further.

 

I say might because it's not suitable for all applications (there are a lot of limitations, specifically in supported extensions and no 5.3 support - yet) but when it works it allows you to get the maximum out of available hardware, how much or little of it you may have.

 

Granted though, I might have better omitted the option in a thread for noobs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.