7 PHP Tips That Will Save You Time & Money

Learning any new language can be a daunting task. I tried to learn Spanish once. I can count to ten and sing the alphabet … oh, and catch me on a good day and I can sing ‘Head, Shoulders, Knees and Toes’ for you. So, when I decided to start learning PHP, the language behind some of the most powerful web applications in the entire world (heard of Facebook or WordPress, yeah that’s PHP) – I was a little overwhelmed.

PHP, a server-side scripting language, can run on several different platforms – including Windows, Linux and Unix – and supports many databases – including MySQL, Informix, Oracle, Generic ODBC and more.

So, for all you new PHP developers out there, here’s a list of seven tips to help you save both time and development costs.

1. Echo is Faster than Print

PHP has two different functions which are usually used to output data to a client – echo and print. Echo is faster than print by about eight percent.

2. Static is Better

If you can declare something to be static, do it. A PHP script will be served at least two to ten times slower by Apache than a static HTML page. So, try to use more HTML pages and fewer server-side scripts. If you are working in OOP, if it can be a static method, declare it static. This will improve your speed by a factor of four.

3. Require() vs. Include()

require() and include() are pretty much identical in every way, except that require stops if the file is missing. However, as far as performance is concerned, there is very little difference. Also, whenever possible, use require() instead of require_once(). Using require is about a 3x to 4x speed-up over using require_once. Also, important to know, require_once() is expensive.

4. Echoing Strings

When you echoing strings it’s actually faster to separate them by a comma instead of a dot. However, you should know this only works with echo – which is a function that can take several string arguments. Also, try to use echo’s multiple parameters instead of string concatenation.

5. Arrays and Single Character Functions

If a function – like a string replacement function – will accept both arrays and single characters as arguments, and if your argument list isn’t too long, try writing redundant replacement statements, instead of a line of code that accepts arrays as search and replace arguments.

6. Stick to Coding Standards

If you stick with coding standards it will make it a lot easier for you to understand other people’s code, and in return they’ll actually be able to understand yours.

7. Cache As Much As Possible

Try using memchached, a high-performance memory object caching system that speeds up dynamic web applications by alleviating the database load. Your PHP scripts are recompiled every time unless the scripts are cached. So, make sure to install a PHP caching product. This will increase your performance by 25-to-100% because it removes compile times.

So there you have it, seven tips that should help you save time and money in your web page design and development. One last tip for you, before you start bugging experts with questions (not that they wouldn’t be happy to help) try doing a Google search. It’s amazing the things you can learn by Googling. You can also connect with PHP Users’ Groups all over the world where smart people gather to chat, help each other and explore ideas.

Nichole Nelson is a web and graphic design student at her local university, when she’s not at school you can find her crafting, cooking and teaching her computer-illiterate husband about credit card processing.

Incoming search terms for the article:

4 Comments

  1. Danny

    01.29.2011

    Nice post, people really need to start appreciating PHP, it gets things done.

  2. axel

    01.29.2011

    thanks.. very good article, don’t know about some points

  3. EllisGL

    01.30.2011

    #2. From a test I’ve seen with PHP 5.2 (don’t know with PHP 5.3), Static methods where actually a little bit slower than Public methods, but used a lot less memory.

    #4. The speed gain isn’t super great. I’ve seen mixed results with commas, periods, single vs double quotes. I used to do this, but ended up dropping it, since I would have to go back and turn the lines into string for use in variables. So even if you free up 10 seconds of processing a month, you end up killing you own time if you have to go back on these.

  4. sokzzuka

    01.30.2011

    Apart from point 6 and 7, other tips are microoptimizations, and unless you’re writting a new facebook, it’s meaningless to use them, because they lower the quality of code.