Give function get file extension for you I have written a function that retrieves the extension from a given file name for you. I would like to make this more efficient and increase it's performance.

&lt;?php
function getExt($filename) {
    //if(!is_file($filename)) return '';
    $n = strlen($filename);
    if($n&lt;2) return '';
    $non_ext = '';
    $ext = '';
    for($i=$n;$i--;$i&gt;=1) {
        $c = $filename[$i];
        if($c==='.') {
            $ext = '.'.$non_ext.$ext;
            $non_ext = '';
        }else
            $non_ext = $c.$non_ext;
    }
    return $ext;
}
$filename = "asdfasdfasd.tar.gz";
$ext = getExt($filename);
var_dump($ext);
?&gt;
You can use PHP inbuilt function. Inbuilt functions are the fastest.

$ext = pathinfo($filename, PATHINFO_EXTENSION);