PHP programming shortcuts: save time and lines of useless code.

Posted: August 12, 2010 at 5:14 pm | (23) Comments

Before I even started writing this, I am sure that the comments will be filled about how consolidating code makes it hard to understand, more braces makes it easier to read, etc. This is for those who think it’s better to have less code, such as yours truly. I like seeing less code; I find it more efficient and much better.

Short-hand mathematical operators
Those are known by most programmers however missed by a lot, the following can be shortened by it’s alternative next to it.

1
2
3
4
$int1 = $int1 + $int2;  -- or alternatively:  $int += $int2;
$int1 = $int1 - $int2;  -- or alternatively:  $int -= $int2;
$int1 = $int1 * $int2;  -- or alternatively:  $int *= $int2;
$int1 = $int1 / $int2;  -- or alternatively:  $int /= $int2;

Counters
Here is another very simple one, but good to know for those who don’t know about it.

1
2
$int = $int + 1; -- or alternatively: $int++;
$int = $int – 1; -- or alternatively: $int--;

Short-hand print statements
Ever find yourself have to write so much just to be able to echo a variable in HTML? No more!

1
<?php echo $string; ?> -- or alternatively,  <?=$string?>

Braces: you don’t always need them.
You don’t always need to use the braces in any if or while statement if it only containers one directive.

1
2
3
4
if ($x)
{
echo "is x";
}

Could just be

1
if ($x) echo "is x";

Ternary operator
My personal favorite, saves a lot of time and I still think it makes readable code

1
2
3
4
5
6
if ($type == 'human')
{
echo 'human';
} else {
echo 'robot';
}

Or why not..

1
echo ($type == 'human') ? 'human' : 'robot';

You can even set variables using that method

1
$type_of_person = ($type == 'human') ? 'human' : 'robot';

Verifying zero values not being false
Sometimes we deal with a function that can return a number and we want to make sure it returns a number, but if it returns 0, PHP will consider it as false.

1
if (getNumber()) { …. }

The number can return 0 and be as false, where as the following will not consider the integer 0 false

1
if (getNumber() !== false) { … }

Alternative control structure syntax
This is a great feature to do when you’re trying to do a lot of PHP work with HTML, it would be hard to list all of them but you can refer to it here: http://www.php.net/manual/en/control-structures.alternative-syntax.php.

Feel free to post any other ones in the comments section.. we all have that one trick we always use!

Related posts:

  1. Handling PHP Errors Your Way
  2. New support system and some free open-source code!
  3. Secure programming habits in PHP
  4. Joomla web hosting: Is it suitable for your website or not?
  5. How to convert/encode files to FLV using FFMPEG & PHP

Comments



Post a comment


What our clients are saying — Read More →

I am a beginner in computers and internet, I bought my first domain name and my web hosting from vexxhost, I was having difficulties installing my blog, your technical support was very patient with me and help me install my blog. I know this is not their responsibility but they do it to help me, thank you support.


Kalie J.
Awards — View More →

Technology Partners