Making a function set a variable

Discussion in 'PHP' started by cammy_dude, Nov 29, 2007.

  1. #1
    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.
     
    cammy_dude, Nov 29, 2007 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    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);
     
    serialCoder, Nov 29, 2007 IP
  3. Elad-A

    Elad-A Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    function upload_file_type($file) 
    {
    	return strtolower(array_pop(explode('.',$file)));	
    }
    
    $ext = upload_file_type('file.name.flv');
    
    echo $ext;
    
    PHP:
     
    Elad-A, Nov 29, 2007 IP
  4. cammy_dude

    cammy_dude Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks, thats what I was looking for =]
     
    cammy_dude, Nov 29, 2007 IP
  5. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    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
     
    bobb1589, Nov 30, 2007 IP