1

At the vary beggining of my script, I declared the variable $errors as an empty array using $errors = array();.

Through the script, inside some functions, I tried to add an element to this array by using $errors['key'] = 'value'; and at the end of the script, whithin another function, I tried to print $errors's values using print_r and var_dump but both returned an empty array.

I also tried with array_push($errors, 'test') just to test - I can't use this method because I need to asign a key to each value - but it did not work either.

To make sure that $errors['key'] = 'value'; was beeing called, I added echo "<script>console.log(\"random message\")</script>"; and I could see "random message" in the console but $errors array was still empty.

Am I adding the values in the wrong way? Is this a scope issue? Should I pass $errors as an argument of the function where I want to print its values or asign it a value?

Example of how I'm trying to add the values:

$errors = array();

function randomFunc() {
    //do some stuff
    if (somethingWentWrong) {
        //If I add 'echo "<script>console.log(\"random message\")</script>";' here, it logs 'random message' to the console
        $errors['new-key'] = 'a string';
        return false;
    } else {
        return true;
    }

function finalFunc() {
    if (randomFunc()) {
        echo "yee";
    } else {
        //If I add data here using '$errors['test'] = 'value';', it works
        print_r($errors); //Returns empty array
        var_dump($errors); //Returns empty array
    }
}



Thanks in advance!

Picoral
  • 199
  • 1
  • 2
  • 13

0 Answers0