0

I have two php files, say

action.php

require_once 'action_helper.php';

storeDataToDb($data); //function from action_helper.php
logPersistIsPerformed(); //function from action_helper
echo $success; //variable set in action_helper.php

action_helper.php

$success = "success";


function storeDataToDB($data) {
    // persist data
}

function logPersistIsPerformed() {
    insertToDB($success);
}

I'm not sure if this is just a scope issue but what I encounter is when action.php calls the functions and variables declared in action_helper.php there are no issues.

but when I call a function in action_helper.php from action.php, which calls a variable declared in action_helper.php, it doesn't seem to see this success variable.

during debugging, once I loaded the page, I get to see all the variables both from action and action_helper. but when I get to step into the function from action_helper, I'm not able to see the variables declared in action_helper but just the variables passed into that function.

chip
  • 3,039
  • 5
  • 35
  • 59

1 Answers1

0

You need to use the global keyword to let PHP know that $success is a global variable.

function logPersistIsPerformed() {
    global $success;
    insertToDB($success);
}
user2182349
  • 9,569
  • 3
  • 29
  • 41