So as I guessed you are using a framework as you said in the comments:
@Rizier123 Yes, I'm using Laravel. Does it matter? – Kai 6 mins ago
And if it matters? Yes it does.
Probably what is happening here is, that the code which you show us here is wrapped into another function somewhere else.
Means that the variables in the Sum()
function are in global scope, but the other ones outside of it not, since they are probably in another function == another scope.
You can reproduce it with this code:
function anotherFunction() {
$a = 1;
$b = 2;
function Sum() {
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
}
anotherFunction();
And if you have error reporting on you will get:
Notice: Undefined index: a
Notice: Undefined index: b
2
Just put the error reporting at the top of your file(s) to get useful error messages:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
To solve this now you have to declare the variables also in global scope, either with:
$GLOBALS["a"] = 1;
$GLOBALS["b"] = 2;
or like this:
global $a, $b;
$a = 1;
$b = 2;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…