I want a good and maybe easy way to organize or code php. Like ways to organize the way a page calls on a global layout or whatever. I am starting to code a new site from scratch and I want to have a good php start. Please give examples, explanations, and whatnot that can help me in my adventure.
For me personally, the best way to code PHP is to keep it neat. Comment your code always, and make sure you develop a system whereby the comments are useful for telling you what the next piece of code does, for example: // -> Establish something (e.g. a MySQL connection) // <- Unsetting variables $x, $y and $z for security purposes. Also, make sure you give your variables useful names, rather than just using something like $asdf. Your names should be telling you (and anyone else who happens to be working on the script) exactly what the variable contains, or the line of code does. Use a Database class! Up until the other day, I was a sceptic. However, it's amazing how it speeds up your MySQL development. One I can personally recommend is Slaout's Advanced Database Class. Lastly, use templates. There's nothing worse than seeing a page full of hardcoded HTML. Smarty is your best bet, it's simple and powerful at the same time. Andy.
A lot of these are designed for phpBB only, but some of them still apply for phpBB - http://area51.phpbb.com/docs/coding-guidelines.html And also, practice makes perfect.
Umm onehundred, are you lost? Also I know to keep it neat and whatnot but how would I go about making templates. The way I used to do it is have a class full of functions with the html layout in it, such as header, container, etc. Then echo it like in every page with $classfulloffunctions->$header(); Is this any good, what are better ways?
Doing things that way effectively defeats the object of templates. The idea of the template is to separate the PHP and the HTML. As I said before, check out Smarty. It's perhaps one of the best freely available PHP classes around. They also have a wealth of information on the website showing you exactly how to use it, and a forum full of guidance if you have absolutely any question relating to Smarty. Also, some of the stuff in phpBB.net's guidelines is very good. The coding practices there are pretty good. I wouldn't advocate using phpBB though - throwing yourself in at the deep end and creating a forum of your own is the only way that you'll become an effective PHP programmer.
Thanks for the tip on Smarty, I've never seen that class before. It looks very interesting and could really clean up some of my previous projects .
Its kinda funny how you're making a similar website as Digital Point ..yet asking how to do it on digital point forums. You have a website where there's a Forum for Programming -> PHP What are you gonna tell your guests if they ask you that question?
The best way to code is to use an MVC approach. Google it. However, there is no need to reinvent the wheel, use an existing PHP framework. All the hard work will be done for you and you can just focus on your web app functionality. Well known ones are : Yii framework Codeigniter Kohana Cakephp Zend Framework All use the MVC approach.
I disagree. MVC frameworks won't help you to learn PHP. Yes, they speed up development, but I wouldn't recommend ever using them if you're hoping to improve your PHP skills. A good programmer will always come up with a more effective system, without the code bloat or restrictive nature of a framework. Personally, I find them to be a lazy method, but that's just my opinion.
Well, he said he wanted to code PHP, not learn PHP. I assume he already knows that Anyway, everyone uses a framework in some way, I'm sure you reuse functionality for different websites don't you? It's not lazy, it's being efficient and making the most of your time. Having others build the core framework helps you stay focused on what is really important, your application and it's functionality. Besides, being fluent in several php frameworks is often a requirement if you are persuing a job in PHP, so it's best that you spend some time working with those frameworks as well.
Yeah, fluency in frameworks is a part of the job, and I keep a huge collection of things ready to reuse. I do however, think it's best that people spend more time working with raw PHP rather than just using the quick and dirty MVC method. MVC gets the job done quickly and effectively, granted, but for me personally the disadvantages to MVC frameworks don't make them attractive to use in clients' projects (unless of course the client requests them). There's no real upside to using frameworks apart from the fact that it cuts out a lot of the basic coding work - i.e. connecting to the database. The file structures on some of the frameworks aren't amazing, either. There are much simpler ways of separating PHP from HTML than for instance, CodeIgniter's model and view directories.
You're right, there's no real upside to using frameworks apart from the fact that it cuts out alot of the basic coding work, like: - DB access - Authentication - Internationalization - Input validation - Caching - Security - Error handling etc, etc.. We'll just agree to disagree. Everyone has their own distinct style. I generally recommend frameworks because alot of people, whether they know it or not, aren't very good at PHP. Their code is a complete mess that is almost always unreadable, insecure and very hard to update. At least with a framework, you have a sort of structure that has to be followed, which enforces at least some security standards and makes it easier for others to continue the work.
I use includes to form the layout (header, footer, sidebar, etc..), and I use a lot of ifs /elseifs inside these includes, to control what shows up for each page. I like it how I do it coz I could just edit 1 page to change something on a different page, even though the include is on all pages, yet, the changes only affect the one page I target. Let's pretend this is inside the include "sidebar.php", & "sidebar.php" is included inside all your site's pages: <?php $basename = basename($_SERVER['PHP_SELF']); if ($basename == "1-page-on-site.php") { echo 'No ads shown, different sidebar shows up, etc'; } else { ?> Ads, normal sidebar.. <?php } ?> PHP: On 1-page-on-site.php, the output would be: No ads shown, different sidebar shows up, etc While on the rest of the pages, the output is: Ads, normal sidebar.. You can basically control any page on your site using this, makes rehauling an easy job.