What kind of headers are allowed to be set from inside a function?
It is OK if I set MIME type for an image from inside a function?
Example:
function createImage($parameters) {
/**
* Here process parameters
*/
// Set header
header ('Content-Type: image/png');
// create the image an destroy it
}
Are there any advantages or disadvantages if setting the header from inside a function? What are the best practices?
There isn’t any technical restriction besides the usual “headers already sent” stuff, except most people won’t expect a function to modify response headers unless it’s specifically designed and documented to do that.
The best practice is not to do it within a function unless it’s clear that headers are being modified, and that it’s not just happening on accident as a side effect.