Basically, I want to make a function, when called, just sets $y=flv or something like that, so that I can use the variable later in the script. when I try this, it echos nothing: function upload_file_type($x) { $parts = explode(".", $x); $number = count($parts); $ext = $parts[$number-1]; } upload_file_type('file.name.flv'); echo $ext; Code (markup): The script is to separate a file extension from the file name. so, I want the function to set $ext to 'flv' as a global variable, so that i can use the filename later in the script.
you should use return ex: function upload_file_type($x) { $parts = explode(".", $x); $number = count($parts); $ext = $parts[$number-1]; return $ext; } echo upload_file_type($$x);
function upload_file_type($file) { return strtolower(array_pop(explode('.',$file))); } $ext = upload_file_type('file.name.flv'); echo $ext; PHP:
function returnExt($file){ $extension = end(explode(".",$file)); return $extension; } //to call the function $ext = returnExt("filename.jpg"); echo $ext; // will output 'jpg'; PHP: this may be a better way