0

I have created a function using PHP which looks like this

<?php

function addImage($imageFolder, $imageName) {

    global $imageName;

    $imageName = 'stackoverflow'.$imageName;

    if (condition) {

        global $imageFolder;

        $imagePath = $imageFolder.''.$imageName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {

            return true;

        }

    }       
}

if(addImage('image/', $imageName)){

    echo $imageName;
    echo $imageFolder;
}

?>

The problem I am facing is that, I am able to get $imageName outside the function but unable to get $imageFolder ( var_dump($imageFolder) displays null). I am new to the global variable and I really don't know if I am using it correctly

Lei Lionel
  • 1,263
  • 13
  • 20

2 Answers2

2

You go the wrong way. You already have the values outside of the function. Assign them to variables to reuse them. global is used to access variables declared outside of the function. Example:

$myVariable = "hello"; 
function test() {
     global $myVariable;
     echo $myVariable;
}
test();
//output: hello

You can view $imageName outside of your function because you set it to a value after the global statement. But this really isn't the right way.

If you want to modify a passed variable, use a reference ( http://php.net/manual/en/language.references.whatdo.php )

You should do something like this:

<?php

function addImage($imageFolder, &$imageName) {

    $imageName = 'stackoverflow'.$imageName;

    if (condition) {

        $imagePath = $imageFolder.''.$imageName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {

            return true;

        }

    }       
}

$imageFolder = 'image/';
$imageName = "..."; // don't know where this comes from in your code.

if(addImage($imageFolder, $imageName)){

    echo $imageName;
    echo $imageFolder;
}
M_T
  • 407
  • 2
  • 6
2

The issue is that the global keyword doesn't make an existing variable a global one. Instead, it tells PHP that when you refer to a variable by that name, it should look in the global scope.

You have two options to fix this:

1. Use different variable names for parameters than you do for the globals

<?php

function addImage($imgFolder, $imgName) {

    global $imageName;

    $imageName = 'stackoverflow'.$imgName;

    if (condition) {

        global $imageFolder;
        $imageFolder = $imgFolder;

        $imagePath = $imageFolder.''.$imageName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {

            return true;

        }

    }       
}

if(addImage('image/', $imageName)){

    echo $imageName;
    echo $imageFolder;
}

?>

2. Use the $GLOBALS variable

<?php

function addImage($imageFolder, $imageName) {
    $imageName = 'stackoverflow'.$imageName;
    $GLOBALS['imageName'] = $imageName;

    if (condition) {
        $GLOBALS['imageFolder'] = $imageFolder;

        $imagePath = $imageFolder.''.$imageName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {

            return true;

        }

    }       
}

if(addImage('image/', $imageName)){

    echo $imageName;
    echo $imageFolder;
}

?>

3. Don't use globals (probably a better solution) [Thanks Robbie Averill]

<?php

function addImage($imageFolder, $imageName) {
    $imageName = 'stackoverflow'.$imageName;

    if (condition) {

        $imagePath = $imageFolder.''.$imageName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {

            return $imageName;

        }

    }  
    return false;
}

$imageFolder = 'image/';
if($imageName = addImage($imageFolder, $imageName)){

    echo $imageName;
    echo $imageFolder;
}

?>
Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50