5 tips to make your PHP code beautiful (1)

Get your brackets right

..and indent your code properly, always. Always give to each of your curly brackets their very own line.
I am taking a snippet from Kohana to demonstrate this.

(bad) What do you think of this code ?

if ( ! is_array($data)){
if (strpos($data, '[') !== FALSE)
{$data = preg_replace('/\[.*\]/', '', $data);
}$data = empty($data) ? array() : array('for' => $data);}

(not nice) This is much better

if ( ! is_array($data)){
	if (strpos($data, '[') !== FALSE){
		$data = preg_replace('/\[.*\]/', '', $data);
	}
	$data = empty($data) ? array() : array('for' => $data);
}

(sweet) A nicely formatted code should show like this. You can tell at a glance where your every if begins and ends.

if ( ! is_array( $data) )
{
	if ( strpos( $data, '[') !== FALSE )
	{
		$data = preg_replace('/\[.*\]/', '', $data );
	}
	$data = empty( $data ) ? array() : array( 'for' => $data );
}

This seems like a great time to subscribe my RSS !

Leave a Reply

Bills

Don’t forget to Subscribe

Latest Activity

Posts

  • Google this is ridiculous
    Google you’re doing it wrong. Very wrong. This is utterly ridiculous. It’s a screenshot with a standard Firefox browser, in the standard screen resolution (1280×800), on Read More
  • A step back from the open source
    One month ago, I created my first Android app. While the app was a paid one, the reception has been outstanding. I’ve gotten a fair amount Read More
  • Facebook shuts down EventPress development.
    When you’re a big company, especially one that makes its money on free web services and advertising, it’s very easy to say you love open Read More
  • Why Android is laggy
    Great post from Andrew Munn explaining some key differences between Android and iPhone/iOs, especially when it comes to rendering and smoothness of animations and why Read More
  • Google Plus keeps your data as much as Facebook does
    When you delete an account from Google+, Google promises you to delete all the data associated with your G+ profile. Well, I deleted mine some time Read More