Php 更改变量';当变量为参数时,函数中的s值
我熟悉scope,但没有太多使用它。如果通过在函数中使用全局Php 更改变量';当变量为参数时,函数中的s值,php,string,function,methods,scope,Php,String,Function,Methods,Scope,我熟悉scope,但没有太多使用它。如果通过在函数中使用全局$variableName知道变量名称,我知道如何在函数中更改变量的值。 我正在编写一个传递两个参数的方法。第一个将接受包含字符串的数组,第二个将保存要执行的设置,例如用于加密和修剪到修剪空间的md5。 有没有办法在函数中更改第一个参数的值?或者你知道有更好的方法来实现这一点吗? function _Edit($string, $rules) { #check if array if(is_array($rule
$variableName
知道变量名称,我知道如何在函数中更改变量的值。我正在编写一个传递两个参数的方法。第一个将接受包含字符串的数组,第二个将保存要执行的设置,例如用于加密和修剪到修剪空间的md5。
有没有办法在函数中更改第一个参数的值?或者你知道有更好的方法来实现这一点吗?
function _Edit($string, $rules)
{
#check if array
if(is_array($rules)!=TRUE)
{array_push($GLOBALS[debug], '<span class="error">_Edits second arguement must be an array</span>');}
if(is_array($string)!=TRUE)
{array_push($GLOBALS[debug], '<span class="error">_Edits first arguement must be an array</span>');}else
{
#loop through the strings
foreach ($string as $sk=>$sv)
{
#make changes based on rules
/* order of rules is important.
the changes will be made in the order the rules are sent */
foreach ($rules as $rv)
{
switch ($rv)
{
case 'md5':
//$string[$sk] = md5($sv);
//GLOBALS[$string][$sk] = md5($sv);
break;
}
}
}
}
}
函数_编辑($string,$rules)
{
#检查数组是否正确
if(is_数组($rules)!=TRUE)
{array_push($GLOBALS[debug],“_Edits第二个参数必须是数组”);}
if(is_数组($string)!=TRUE)
{array_push($GLOBALS[debug],“_editsfirst arguement必须是数组”);}否则
{
#在字符串中循环
foreach($sk=>$sv的字符串)
{
#根据规则进行更改
/*规则的顺序很重要。
更改将按照规则发送的顺序进行*/
foreach($rv规则)
{
交换机($rv)
{
案例“md5”:
//$string[$sk]=md5($sv);
//全局变量[$string][$sk]=md5($sv);
打破
}
}
}
}
}
为什么不返回最终在函数中修改的数组
所以
在您的功能中,您可以:
function _Edit($string, $rules) {
... your code ...
... modify $string ...
return $string;
}
如果我理解正确,您想从函数内部更改函数外部第一个参数的值吗?为此,您必须通过引用传递,或者只需返回更新的数组并覆盖原始值
好的,这就是我应该做的。我没有意识到我可以用返回的数组更新数组。
function _Edit($string, $rules) {
... your code ...
... modify $string ...
return $string;
}