Tag Archives: Initialisms

PHP Coding Style: Large IF Statements

Have you ever seen some PHP code like that:

1
2
3
if ($condition1 && $condition2 && $string1 == $string2 . $string3 && $string4 != $string5 . $string6) {
   // write some code here!?
}

It is ugly, isn’t it?

I’d prefer to make it clearer! What about that?

1
2
3
4
5
6
7
8
9
if ($condition1 
   && $condition2
   && $string1 == $string2
                . $string3
   && $string4 != $string5
                . $string6
) {
   // write some code here
}

PHP Associative Arrays Coding Style

No PHP programmer writes code without arrays. Sound strange, but as many programmers there are, as many ways to format the array notation there are.

My advice is to rely on a defined standard. I personally use the Zend Framework’s coding style.

So how to format the code? The first way is on the same line:

$myArray = array('key1' => 'value1',
		 'key2' => 'value2');

And the other, perhaps more clear way is to place the associative pairs on a new line:

$myArray = array(
	'key1' => 'value1',
	'key2' => 'value2',
);

Note that in the second example there’s a comma after the second pair. This is correct PHP syntax and is strongly encouraged!

It’s up to you which way to take. However it mainly depends on the case, but please use standards.