I am trying to change a variable that is outside of a function, from within a function. Because if the date that the function is checking is over a certain amount I need it to change the year for the date in the beginning of the code.
我试图改变一个函数之外的变量,从函数内部。因为如果函数正在检查的日期超过一定数量,我需要它在代码开始时更改日期的年份。
$var = "01-01-10";
function checkdate(){
if("Condition"){
$var = "01-01-11";
}
}
4 个解决方案
#1
41
A. Use the global keyword to import from the application scope.
使用全局关键字从应用程序范围导入。
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
checkdate();
B. Use the $GLOBALS array.
使用$GLOBALS数组。
$var = "01-01-10";
function checkdate(){
if("Condition"){
$GLOBALS['var'] = "01-01-11";
}
}
checkdate();
C. Pass the variable by reference.
通过引用传递变量。
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
#2
44
Just use the global
keyword like so:
使用全局关键字,比如:
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
Any reference to that variable will be to the global one then.
对该变量的任何引用都将指向全局变量。
#3
8
All the answers here are good, but... are you sure you want to do this?
这里所有的答案都很好,但是……你确定你要这么做吗?
Changing global variables from within functions is generally a bad idea, because it can very easily cause spaghetti code to happen, wherein variables are being changed all over the system, functions are interdependent on each other, etc. It's a real mess.
从函数内部更改全局变量通常不是一个好主意,因为它很容易导致通心粉代码的发生,其中变量在整个系统中都被更改,函数相互依赖等等。
Please allow me to suggest a few alternatives:
请允许我提出几个选择:
1) Object-oriented programming
1)面向对象编程
2) Having the function return a value, which is assigned by the caller.
2)让函数返回由调用者分配的值。
e.g. $var = checkdate();
例如:$ var = checkdate();
3) Having the value stored in an array that is passed into the function by reference
将值存储在数组中,并通过引用传递给函数
function checkdate(&$values) { if (condition) { $values["date"] = "01-01-11"; } }
函数检验日期(&$values) {if (condition) {$values["date"] = "01-01-11";} }
Hope this helps.
希望这个有帮助。
#4
5
Try this pass by reference
试试这个推荐信
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
or Try this same as the above, keeping the function as same.
或者和上面一样,保持函数不变。
$var = "01-01-10";
function checkdate($funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate(&$var);
#1
41
A. Use the global keyword to import from the application scope.
使用全局关键字从应用程序范围导入。
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
checkdate();
B. Use the $GLOBALS array.
使用$GLOBALS数组。
$var = "01-01-10";
function checkdate(){
if("Condition"){
$GLOBALS['var'] = "01-01-11";
}
}
checkdate();
C. Pass the variable by reference.
通过引用传递变量。
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
#2
44
Just use the global
keyword like so:
使用全局关键字,比如:
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
Any reference to that variable will be to the global one then.
对该变量的任何引用都将指向全局变量。
#3
8
All the answers here are good, but... are you sure you want to do this?
这里所有的答案都很好,但是……你确定你要这么做吗?
Changing global variables from within functions is generally a bad idea, because it can very easily cause spaghetti code to happen, wherein variables are being changed all over the system, functions are interdependent on each other, etc. It's a real mess.
从函数内部更改全局变量通常不是一个好主意,因为它很容易导致通心粉代码的发生,其中变量在整个系统中都被更改,函数相互依赖等等。
Please allow me to suggest a few alternatives:
请允许我提出几个选择:
1) Object-oriented programming
1)面向对象编程
2) Having the function return a value, which is assigned by the caller.
2)让函数返回由调用者分配的值。
e.g. $var = checkdate();
例如:$ var = checkdate();
3) Having the value stored in an array that is passed into the function by reference
将值存储在数组中,并通过引用传递给函数
function checkdate(&$values) { if (condition) { $values["date"] = "01-01-11"; } }
函数检验日期(&$values) {if (condition) {$values["date"] = "01-01-11";} }
Hope this helps.
希望这个有帮助。
#4
5
Try this pass by reference
试试这个推荐信
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
or Try this same as the above, keeping the function as same.
或者和上面一样,保持函数不变。
$var = "01-01-10";
function checkdate($funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate(&$var);