5 Handy PHP Tips and Tricks


We are delighted to welcome our first guest author to The Web Design Blog. Darren Pinder is a Web Designer working out of Glossop, Derbyshire and in his first article he shares five very handy PHP tips.

Darren set up Online Selling in January 2009 providing website design and eCommerce websites.

5 Handy PHP Tips and Tricks by Darren Pinder

PHP will be celebrating its 15th birthday this year, and now is a great time to look back and wonder at how it’s managed to become such an integral part of internet design and application during that time.

It has become one of the first choice languages of web application developers, the go-to language if your website needs something more than what HTML can give you. Over 20 million domains have PHP installed, and it is considered the most popular Apache HTTP Server module by far. Some of the most famous examples of PHP at work are Facebook, Wikipedia, Yahoo!, Digg, Joomla, WordPress, YouTube, and more. PHP has become part of the internet.

For the anniversary of PHP, I decided a “5 Useful Tips” list was in order, to help you use PHP to its full potential (and to avoid some of the problems inherent in the language!). I hope you find these useful.

1. PHP memory allocation

PHP scripts have a memory allocation attached to them by the server. To stop a PHP script taking up too much processing power, usually an arbitrary Mb usage is allocated. Sometimes this is a hinderance to web developers, so to increase the amount of memory allowed, enter the following line into the php.ini file on the root of your website. If the php.ini doesn’t exist, you can simply create it. You can increase this number if you still need more memory, but you may find an upper limit set by your host.

memory_limit=16M

2. PHP include functions

PHP can be used to great effect on websites which don’t involve any database interaction. By using the PHP include function, you can include another .php file anywhere in the current file. This can be extremely useful to include, for examples, headers, or footers, or a left menu that stays fixed. Instead of having to make changes to each file in the future, you can change the one PHP included into all the others. The syntax for including a file is as follows:

<?php include ('directory/file.php'); ?>

3. Setting variables in PHP

Another really useful PHP tip is to create variables, which can be a huge time saver later on down the line. Do you have to regularly enter a phone number or email address? By storing bits of information into variables, you can change the information and once and have it propegate site-wide instantly. No more Find and Replace, we say. To store a variable, use the following code:

$variable_name = 'variable_result';

And to call the variable, use:

<?php echo $variable_name; ?>

4. Using if statements in PHP

Using if statements in PHP can be incredibly useful for only showing information you really want to. The potential that an if statement can give you is huge, and the syntax is incredibly easy to use. For example, if the variable telephone1 is defined, then show it:

<?php if
("$telephone1" != "") {
	echo ("$telephone1");
} ?>

5. Difference between single and double quotes

In PHP there is a difference when using either single or double quotes, either ‘ or “. If you use double quotes ” then you are telling the code to check for a variable. If you are using single quotes ‘ then you are telling it to print whatever is between them. This might seem a bit trivial, but if you use the double quotes instead of the single quotes, it will still output correctly, but you will be wasting processer time.

So that’s it, 5 useful and hopefully time-saving tips for you. I’d love to hear any feedback you have on these solutions, whether they worked for you, or whether you have another useful tip to share with others.

You can view more of Darren’s work at Online Selling where he provides website design and eCommerce websites.

VN:F [1.9.13_1145]
Rating: 2.4/5 (41 votes cast)


49 Responses

26th Jan 2010

Hmm, the comment system has interpreted the php tags in my last comment. What I had meant to say was:

Shouldn’t #3 be echo $variable_name?

26th Jan 2010

Thanks for pointing that out Chris!

Thanks for these great tips – they are all very relevant and useful PHP stuff :) .

28th Jan 2010

Back to the basics, good article for starting off with php. I’d read another “next 5 php tricks” where you cover an array, a foreach, a while, a function and an object.

28th Jan 2010

Useful PHP tips, thanks for the information.

1st Feb 2010

Congrats on the guest post spot, Darren! One common item I’d add to the list is the use of PHP for printing the current year, which is very useful for site footers: ?php echo date(“Y”) ?

1st Feb 2010

Thanks for all the great feedback, I’m taking down all the suggestions for a possible follow-up article.

3rd Feb 2010

Very useful tips! I would like to read some more if is possible to publish another article. Thank you!

8th Feb 2010

I like the tips sounds like you know what your talking about thanks

18th Feb 2010

Some nice little tips there!

Another one that I use all the time is short tags (however you need to make sure the support for them is enabled on your server).

Instead of having:

You can use:

This is useful when pulling data out into tables etc.

25th Feb 2010

Thanks for those tips always come in handy when battling with tedious code!

1st Mar 2010

Very handy and useful tips on PHP. Thanks for the article!

2nd Mar 2010

You forgot to mention Drupal. When you list scripts like Joomla, WordPress, you shouldn’t forget to mention Drupal. It is taking over most of the biggest websites in Internet Web like BBC, Universal etc. Cheers PHP :)

7th Mar 2010

Thanks for that Darren, I didn’t know that PHP evaluated differently depending on the type of quote used.

9th Mar 2010

Thanks for all these tips, I am working on a custom templating system and the include function has worked well when using OOP methodology :)

12th Mar 2010

Nice article, am just learning php so good to read this. Thanks

15th Mar 2010

This is a good brief summary of some of the benefits of PHP!
I have been coding in php for 4 years and had never come across the memory allocation function! Great Post!

6th Apr 2010

Hi Darren, thanks for the great set of PHP tips. I know many designers who struggle with server side scripting and useful tips and tricks like these can help save a lot of frustrating hours.

13th Apr 2010

For code efficiency reasons you should probably update this post with why readers should use include_once() or require_once() instead of include(). This matters when more and more visitors hit their application as traffic increases. Nice rundown of basic concepts though.

22nd Apr 2010

Very useful and informative tips. Like to learning PHP in deep.

28th Apr 2010

Nice Article…., Thanks..

16th Jun 2010

A very useful and well written article for junior programmers.

18th Jun 2010

Some good information there cheers guys.

27th Jun 2010

These are really, really simple “tips”. This is usefull of course, but it is just basic usage of the language.
A more appropriate title should have been “5 php tips for beginners”, in my opinion.

4th Jul 2010

A great starter, but covered the basics…maybe have a next version which covers 5 tips for intermediate php coders? I thought the last point about quote marks was very good :D

Maybe also in a future tip mention @ to supress error messages?

25th Aug 2010

Nice ones, Darren. Welcome onboard, looking forward to your next posts.

25th Aug 2010

Thanks for the useful tips, Darren! I’d like to read some more if is possible to publish another article on the same subject.

28th Aug 2010

Very useful article, I agree with the you should always include a settings file with the absolute path to where you are running your scripts from. This then allows you to do

9th Sep 2010

Awesome and useful PHP stuff, thanks a lot!

3rd Oct 2010

Many thanks although these weren’t really “tips”… rather a very basic overview of what you can do with PHP.

24th Oct 2010

Good tips for people starting out with php. You should create a reference library or something. Maybe have a tip per week in a newsletter?

24th Oct 2010

really nice thx , I was getting errors , cus of that single and double ” difference.

24th Oct 2010

Awesome stuff and Thank you for sharing.

26th Oct 2010

great article, im relatively new to php so some handy tips in there

8th Nov 2010

Some handy tips here that should prove useful for all web designers not currently already using php. The php include has proved to be one of the most useful bits of php code effectively replacing the need for templates. The variables function is another great time saver and something that will come in very handy. Would love to see another follow-up article with some more useful php time saving tips. Great article.

22nd Nov 2010

This is very good article and great tips of Handy PHP.

thanks for sharing.

25th Nov 2010

Awesome, I was wondering , if my hosting has set a upper MB size for Frist tip , Mem Allocate I mean , what Can I do ?

6th Dec 2010

Same problem here , I am using wordpress and I have a limitation of 8MB for uploads I tried Mem Alloc and changed it to 1GB but I still see the 8 MB limit , any suggestion ?

Even thought this post is a bit old , it’s pretty useful.

7th Dec 2010

Good stuff. Effective & useful tips for the professional designers to improve their skills. Will follow these tips & tricks from our next projects. Thank you very much for sharing this post.

22nd Dec 2010

Thanks for the useful tips, Darren! I’d like to read some more if is possible to publish another article on the same subject.

Good Explanation. Really useful for beginners.

I’m just starting to learn PHP and these tips will hopefully come in handy.

Thanks!

14th Feb 2011

Awesome, I was wondering , if my hosting has set a upper MB size for Frist tip , Mem Allocate I mean , what Can I do ?

6th Jul 2011

some good tips here that should prove useful for all web designers not currently already using php

10th Aug 2011

Thanks for sharing these great tips. Very useful ones.

23rd Aug 2011

I often use the Memory Limit script on WordPress for the nextgen gallery. Also the php include function changed my view of website building a few years back, saves so much time. Thanks.

25th Oct 2011

These tips and tricks are helpful for PHP developers

18th Jan 2012

I am just a fresher in this field and its been great to know about. Worthy tips and tricks shared!

25th Jan 2012

Thanks Darren for the “include functions” tip, I’ll be needing that one soon, thanks.

Leave Your Response

* Name, Email, Comment are Required

Follow Us On Twitter

Web design services from Weymouth Web Design
Website Design Northampton

Waterloo Web Design

Web Design Company

Web Design Coventry Blue Monkey provide a complete, results focused web design and SEO service

Graphic River Files

  • PNG Lock Icon Collection
  • Multi Coloured Paint Brush PNG Icons
  • Christmas Bauble Decorations
  • Notebook Icon in multiple colours