I'm currently working on building a forum using php. I'm still rather new to the language (only been using it for a couple of months), and was wondering if anyone had any suggestions and/or tips. Also if it's allowed (I'm not very familiar with the rules around here yet) to post some code from time to time and get feedback.
Something that just came to mind; on some forums I've seen people commenting negatively (and sometimes rather hostilely) to echoing html. Of course no one ever really explains why this is a good or bad practice. I was wondering if anyone could explain the pros and cons. Also if it is a bad practice what could I do instead?
echo is for outputting php variables mostly. Just add the tags between the html tags. Web pages can be formatted neater by not echoing the HTML (because you just write it like a static page) but it is always down to personal preference.
Do you have anything planned? You first must plan the databases (the tables) and classes you'll use, and then other thing like visual, configuration, structure...
I've already got my database and tables laid out. I'm pretty solid on structure, and oh kay on configuration. I would have to say the weakest area at the moment is visual. I hadn't planned a particular look for the forum or site overall, and I'm having a hard time deciding. As far as the inner workings it's all come down to me banging out the code. I'm a little hesitant because I want to do it neat and right on my first try. So to keep my html neat I guess I'll just close php and write it, and open php back up to echo out any dynamically generated html?
Well my tip is of course : Use OOP (Object oriented programming), write in PHP5.
And best luck in your adventure with PHP :)
If you will need some backup go ahead: walas-13 [at] o2 [dot] pl. I'm doing stuff in PHP for over 2 years so i may be useful :D
Some initial hints:
* use PDO and bind variables (security & performance)
* use some template engine or find at least a decent way to separate the view
* consider using SPL, discussion boards can make use of nice iterators ;)
* put enough time in your db design. the php code for boards is not that tricky, the db limits your scalability.
@ "why not echoing HTML":
* it is a bad separation of code and output
* it is harder to maintain
* it is a pain to translate or to support themes
* it leads to clauses all over your code if you reuse functions with direct output on different occasions
* code reuse in general suffers
* it's nearly impossible to add partial output caching (I don't mean the output buffer, but (e.g. group based) caching of generated snippets for high-load sites)
* functionality is bound to one media. if you need parts for a (mobile?) API, XML output or any other interface/RPC type later on, duplication or even more clauses, modes, ...
So as stated above, a template engine is a good idea. If you prefer Smarty, Zend Views or anything else is a discussion of its own, but echoing or suspending the interpreter leads to more problems.