The answer given elsewhere,
$tmp = explode('.', $fileName);
$file_extension = end($tmp);
is correct and valid. It accomplishes what you are trying to do.
Why?
The end()
function does not do quite what you think it does. This is related to how the PHP array
data structure works. You don't normally see it, but arrays in PHP contain a pointer to a current element, which is used for iteration (like with foreach
).
In order to use end()
, you must have an actual array, which has attached to it (normally, invisibly), the current element pointer. The end()
function physically modifies that pointer.
The output of explode()
is not an actual array. It is a function output. Therefore, you cannot run end(explode())
because you violate language requirements.
Simply setting the output of explode()
in a variable creates the array that you're looking for. That created array has a current element pointer. Now, all is once again right in the world.
So what about the parentheses?
This is not a bug. Once again, it's a language requirement.
The extra parentheses (like end((explode()))
) do more than just grouping. They create an inline instance variable, just like setting the function output to a variable. You may think of it as a lambda function that is executed immediately.
This is another correct and valid solution. It is perhaps a better solution, as it takes less space. A good reviewer or maintainer should grok what you're trying to do when they see the extra parentheses.
If you use a linter or SCA program like PHPCS, that may dislike the extra parentheses, depending on the linting profile you're using. It's your linter, tell it what you want it to do for you.
Some other answers also list things like the spread operator or array_key_last()
, which are also reasonable solutions. They may be perfectly valid but they're more complicated to use and read.
I'll just use the @
prefix
This solution is valid, however incorrect. It is valid because it solves the problem. That's about the end of its merit.
Suppressing errors is always bad practice. There are many reasons why. One very large one is that you are trying to suppress one specific error condition (one that you have created), but the error suppression prefix suppresses all errors.
In this case, you will probably get away with this. However, engaging in bad programming habits is cheating and will likely lead you to cheat more and bigger in the future. You will be responsible for bad code. But I'm not the Code Police and it's your code. It's valid because it solves the problem.
Okay, so what's the best answer?
Do what @ryeguy suggests. Don't do string manipulation to solve a well-defined problem that the platform already solves for you. Use pathinfo()
.
This has the added benefit that it actually does what you want, which is finding the extension on a file name. There is a subtle difference.
What you are doing is getting the text following the final dot. This is different from finding the file extension. Consider the file name, .gitignore
. PHP knows how to handle this. Does your code?
Once again, I'm not the Code Police. Do what suits you best.