what's better?? haveing a big php page with a lots of functions or having a lot of different pages for each function?? i used to put everything in a big php page with lots of if else.... but now starting to adopt a different style by breaking it into smaller php pages but i wanted to know what's better? in terms of speed,security(if any) and bandwidth...
I do it this way: I have one functions.php file with all common functions that are frequently needed. Then I split the rest into smaller files, and put similar functions in the same files, if possible. Classes go all in separate files called classname.class.php. (This is handy for the __autoload() function.
It's all about organizing your code in order to enable efficient coding, easily adaptable infrastructure and quick error diagnostics.
Well, I have a couple of basic includes right on top, like the database class, the common functions, etc. After that, I have a "engine.php" which does all the work. And on top of the index.php I include the engine, which control the output and all the functions. The content pages (forms, templates, etc.) are in a separate folder "Templates". This proved to be a very efficient system on my last ~5 projects (making small improvements).
I always include a config.php file that carries all kinds of variables necessary for site setup as well as a database connection (every project is db based ... period). This file also includes all my standard functions that I use regularly and can't live without. Now that you see inside my config.php file, the rest looks like this. <?php include 'config.php' ; ?> <html> <title></title> <?php include 'top.php' ; ?> <?php include 'left.php' ; ?> <p> meat of the page </p> <?php include 'right.php' ; ?> <?php include 'bottom.php' ; ?> PHP: You don't have to be too bright to figure out what goes into the other includes. Each page ends up looking quite clean in the end. I'm sure there are better ways, I'm actually interested to read the rest of the your page structures as I'm always looking for a better way.
I think you find things that work better over time. I generally stick to what everyone is saying here as far as including functions, etc. I'll also sometimes chop blocks of code out of a page so I can use it repeatedly from different pages but stuff like that you have to figure out as you code. I have never noticed useage of the CPU increasing when something is included as opposed to it being in the same file, but I could be wrong. PHP is pretty efficient.
yeah i have all common functions and routines in a config gile that i always include... the style philopoemen is the style i am currently adopting ..saw that phpfusion(a cms) do something like that... thx for ur advice...
I am completely agree with ErectADirectory I think its the best way. save lots on time on coding and i seen this method is used in lots of open source CMS like Joomla.
Like you see many opinion are exposed here, and as johngodfrey said, you will find your way with time. Nico's opinion would be close to best practices recomendations, but there's nothing wrong in having your functions and code in the same file. Personally, i would consider your approach too mouse scroll consumming, and i would tend to do thing like ErectADirectory does, but if you like it that way, then continue with it! Consider also to plan to take a look to PHP class programming capability, this could save you a lot of code.
yep will try to do my php programming in a more object-oriented manner...thought about it long time ago but never done it... thx for the advice
This is exactly what I do. In terms of general coding style I use the following: All braces { } start on a new line All text code is in single quotes not double [helps for speed] Class names always start in upper case and every word starts in UC also i.e. class PageRank {} variable names are all lowercase I think thats pretty much the way I code, hope it helps