How do I code a function that uses external variables? For example? I have an upload script that I am using repeatedly through a few pages. I'd like to make my code more efficient and just define the upload script as a function and call it every time I need to upload a file.

Is it possible to call a function that has to use external variables like the $_FILES[] variable?

The variables $_FILES, $_GET, $_POST, $_REQUEST, etc. are what's known as super-globals. They are special PHP variables that are available everywhere and are based on the current request PHP so even if the function is in a different file as long as it is the same request they'll be populated with the correct data.

So I don't have to define them in the function parameters to be able to use them?

So I don't have to define them in the function parameters to be able to use them?

Nope.

function somefunction()
{
   $somefile = $_FILES['hello']; // works perfectly fine.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.