As the others have mentioned, it is easier to read when it becomes more complex. Take the following for instance:
Although this may seem nicer because it is shorter, we also run into readability issues. As a programmer you should be aware of the amount of memory used for your applications, but not paranoid. If it reads nicer, and also easier to debug, by utilising a couple more variables then I would say take that approach.
We could thus refactor the above to something like the following:
php Code:
$szFilename =
'image.jpg';
$aParts =
explode('.',
$szFilename);
$szExtension =
end($aParts);
$szExtension =
strtolower($szExtension);
printf('The extension is: %s',
$szExtension);
Now you may think that's quite wasteful. However, in the first instance we are assuming everything will be fine and dandy, but being programmers, we know it's never that simple, and so now we've broken it up, we can add some conditional statements to check if everything really is fine and dandy:
php Code:
$szFilename =
'image.jpg';
$aParts =
explode('.',
$szFilename);
if(count($aParts) ==
1){ printf('Unable to find the extension for %s',
$szFilename);
return;
}$szExtension =
end($aParts);
$szExtension =
strtolower($szExtension);
printf('The extension is: %s',
$szExtension);
Additionally, if you show let PHP notify you of every little thing, you will see that PHP do not recommend encasing functions within functions, as in our first example.