5 tips to make your PHP code beautiful (2)

Thoughtful minimalism

Think about the most concise, clear and readable way to write the code your writing. Assume you were writing a function to get a file extension (.gif, .doc, etc.). You could be tempted to use string manipulation functions.

$filename = 'file.doc';
$dot_position = strpos( $filename, '.' );
if ( $dot_position !== false )
{
	$extension = substr( $filename, $dot_position + 1  );
}
var_dump($extension)

It’s a lot of code for such a simple task. Beside that, files with multiple dots ( filename.with.dots.html ) won’t be handled properly.

$filename = 'file.doc';
$extension = array_pop ( explode( '.', $filename ) );
var_dump($extension)

The lesson here is to restlessy try to find the best solutions , even when you thought you knew how to accomplish the task. The one-liner above, is more easy to read and much less error-prone. It will work with file with multiple dots or with no extension without any problem. (note: you may want to filter $filename with basename() to avoid directory names dot to interfere).

I am gonna give another example – imagine you’re trying to print a different string for a bunch of pages on you’re site.

if ( $page_id == 32 || $page_id == 10 || $page_id == null )
{
	echo 'inactive';
} else {
	echo 'active';
}

While this code is pretty ok, you could note that $page_id is repeated one time for every page you want to set as inactive.

$inactive_pages = array( 32, 10, null );
if ( in_array( $page_id, $inactive_pages ) )
{
	echo 'inactive';
} else {
	echo 'active';
}

This way you can add exceptions just inserting a new element in $inactive_pages array. This will make easier refactor the code to read $inactive_pages from a configuration file or from a database.
In situations where you need to keep your code even smaller you could even write a one-liner (but don’t exceed with them, as many people find them less readable – and often they’re right):

echo in_array( $page_id, array( 32, 10, null )  )  ? 'inactive' : 'active';

This seems like a great time to subscribe my RSS !

One response to “5 tips to make your PHP code beautiful (2)”

  1. felix

    $ext = end(explode(‘.’, $filename));
    just less verbose

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