Elye Elye - 26 days ago 7
PHP Question

Why can't my include function get the global variable value?

I have a func.inc file as below

// STATUS_VALLUE
$status_failure = 0;
$status_success = 1;

function resultResultJson($status) {
echo $status;
echo $status_success;
}


And my function.php as below

<?php
include("./includes/func.inc");
resultResultJson($status_success);
?>


I'm expected result
11
. But I got
10
. Why is the function
resultResultJson
not getting the right
$status_success
result?

Updated

The http://stackoverflow.com/a/16959577/3286489 provides explanation on the variable scope, which explains why, but doesn't give a resolution. The below answer by @Niyoko Yuliawan helps.

Answer

You need to declare $status_success variable as global variable.

function x($status){
    global $status_success;
    echo $status;
    echo $status_success;
}