Is it possible to access globals inside a function that was called via a URL - like this:
URL
example.com/download.php?fn=download&fname=file
Does the first example below not work because the function call goes straight to the download()
function without reading the rest of the php script? (i.e. where $var1, $var2
are declared).
download.php
<?php
$var1 = 'foo';
$var2 = 'bar';
global $current_user; // from elsewhere
if ($_GET['fn'] == "download")
if (!empty($_GET['fname']))
download($_GET['fname'], $current_user);
function download($fname, $current_user)
{
// Access the global variables in this scope
global $var1;
global $var2;
$output = $fname . $var1 . $var2;
echo $output // no output here...
}
But this works with the same URL as above:
<?php
global $current_user; // from elsewhere
if ($_GET['fn'] == "download")
if (!empty($_GET['fname']))
download($_GET['fname'], $current_user);
function download($fname, $current_user)
{
// Access the global variables in this scope
$var1 = 'foo';
$var2 = 'bar';
$output = $fname . $var1 . $var2;
echo $output // "filefoobar"
}
Is there a way of doing this, or do I just have to declare my variables in the function and skip using global
? (I know global
is often ill-advised etc...)