Friends
| Don't reinvent the wheel |
|
|
|
| Written by Nate Lyman |
| Wednesday, 20 January 2010 06:53 |
|
Once you have been doing any programming language for an extended period of time, including looking at/reviewing other people's code, something becomes apparent. There are a lot of developers who try to reinvent the wheel. This can be especially bad with PHP. PHP has a lot of great built in functions, don't try to mimic them, use them. You'd be surprised, but PHP usually has you covered. I thought it'd be helpful to point out the top offenders that I have ran into.
1. Word Wrapping
I have seen various word wrapping functions written over and over again. Use PHP's built-in wordwrap() function? It works well, and it is faster than anything you could write in PHP. Usage: $string = "Some really long string about something your user really cares about."; This will insert a HTML line break tag every 20 characters on a word boundary. Optionally you can pass in ($cut = TRUE|FALSE) and make it do a hard cut off in the middle of words.
2. Arrays - specifically array_merge() and array_chunk() A big reason why I love PHP is because of all the built in support for processing Arrays of data. If you develop in PHP and haven't spent 30 minutes going through all of the Array functions, do it now. You'll thank me later.
3. Stripping HTML Tags out of text. I enjoy writing regular expressions as much as the next guy. But seriously, strip_tags() works really well. The thing I like most about strip_tags() is that it lets you allow tags of your choice. For example you have some HTML, but you still want to keep formatting (<p>,<em>,<strong>, etc...). strip_tags does it really easily: $string = "Some amount of HTML"; The one word of caution though. It doesn't remove javascript code or CSS from between the tags, so I usually add <script> and <style> to the exclusion list and then remove it using a regular expression. 4. Frameworks There are a lot of great frameworks out there, I encourage you to learn one or two and use them. It will put you at a disadvantage if you make your own. I am guilty of it myself, but think of it this way: What is more marketable to a future employer? Zend Framework or "Nate's really awesome framework". The framework you might build will probably work well for you, because you designed it to omit features/functionality that have annoyed you in the past. But rest assured, if you release it to others, there will be people that find your Framework horrible. So kids, learn Zend/CakePHP/Symfony, your resume will appreciate it. |
| Last Updated on Thursday, 21 January 2010 17:22 |



