Jim from Oakland Posted December 17, 2016 Share Posted December 17, 2016 Am getting back to coding after a while away. I remember a lot but cannot remember even more. In a variable within a class I have a file name. I want to extract the file name EXTENSION. I am using end (to get last array entry) with explode (which makes 2 element array containing 1. the file name w/o extension and 2. the extension itself) as follows: $sFileExt = end(explode('.', $FileName)); I get this notice: Only variables should be passed by reference. I did try making the delimiter (a period char) a variable too. Same message. I does work by the way. Quote Link to comment https://forums.phpfreaks.com/topic/302760-correct-use-of-end-with-explode/ Share on other sites More sharing options...
ginerjm Posted December 17, 2016 Share Posted December 17, 2016 Hmmm... Are you sure the error message applies ot that line number? Quote Link to comment https://forums.phpfreaks.com/topic/302760-correct-use-of-end-with-explode/#findComment-1540450 Share on other sites More sharing options...
kicken Posted December 17, 2016 Share Posted December 17, 2016 From the manual: Parameters array The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference. Quote Link to comment https://forums.phpfreaks.com/topic/302760-correct-use-of-end-with-explode/#findComment-1540451 Share on other sites More sharing options...
Jacques1 Posted December 18, 2016 Share Posted December 18, 2016 There's no need to invent your own file extension function. This is a standard PHP feature: $file_extension = pathinfo($file_name, PATHINFO_EXTENSION); This is both correct and readable, so it's always preferrable over the end() hack. Quote Link to comment https://forums.phpfreaks.com/topic/302760-correct-use-of-end-with-explode/#findComment-1540453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.