Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 函数中的引用参数(&;a),好还是坏?_Php_Function_Parameter Passing_Pass By Reference - Fatal编程技术网

Php 函数中的引用参数(&;a),好还是坏?

Php 函数中的引用参数(&;a),好还是坏?,php,function,parameter-passing,pass-by-reference,Php,Function,Parameter Passing,Pass By Reference,我们编写了两个验证函数,其中一个如下所示(它一次获取所有字段): 另一个函数如下所示: function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {} 第一个函数有几个代码行,并且大大减少了代码行(大约30-35行或更多),另一方面,第二个没有引用的函数增加了代码行(大约30-35行或更多) 我们必须为每个要验证的字段调用第二个函数,但第一个函数(check_fields)正好相反。 我很久以前在一篇文章中读到过,从性能的

我们编写了两个验证函数,其中一个如下所示(它一次获取所有字段):

另一个函数如下所示:

function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {}
第一个函数有几个代码行,并且大大减少了代码行(大约30-35行或更多),另一方面,第二个没有引用的函数增加了代码行(大约30-35行或更多)

我们必须为每个要验证的字段调用第二个函数,但第一个函数(
check_fields
)正好相反。 我很久以前在一篇文章中读到过,从性能的角度来看,带有引用参数的函数是不好的


现在我们不知道该使用哪个函数。从性能角度看,哪一个更好?

使用更易于使用和维护的解决方案。 你说的是微观优化,这是非常无用的。

使用引用,因为在您的情况下,这是一个更简单的解决方案,需要更少的代码。

好吧,我想我自己在一些网络搜索后得到了答案:


当你打电话时,你只要记住这一点:

   <?php 
    $a = 1 ;
    echo "As initial, the real 'a' is..".$a."<br/>";
    $b = &$a ; // it's meaning $b has the same value as $a, $b = $a AND SO $a = $b (must be)
    $b += 5;
    echo "b is equal to: ".$b." and a equal to: ".$a."<br/>";
    echo " Now we back as initial, when a=1... (see the source code) <br/>";
    $a = 1 ;
    echo "After we back : b is equal to: ".$b." and a equal to: ".$a."<br/>";
    echo "Wait, why b must follow a? ... <br/> ..what about if we change b alone? (see the source code)<br/>";
    $b = 23; 
    echo "After we change b alone, b equal to: ".$b." and a equal to: ".$a."<br/>";
    echo " WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! </br>" ;
    echo "to 'clear this, we use 'unset' function on a (see the source code)<br/> ";
    unset($a);
    $b = 66;
    $a = 1;
    echo "Now, after unset,b is equal to: ".$b." and a equal to: ".$a."<br/>"; 
    ?>
审查:

为什么
$a
发生了变化?这是因为
&
(与符号运算符)将
&
作为参考变量,因此它存储在“临时内存”中。当您初始化
$b=&a
时,请参见我的注释
$b=$a
以及
$a=$b
,这意味着无论何时修改
$b
$a
都会被修改,反之亦然。他们被锁链锁住了!这就是引用运算符的简单含义

也许一开始会让人感到困惑,但一旦你成为这方面的专家,你就可以使用这个操作符来实现如下“切换”功能:

<?php

function bulbswitch(&$switch)
{
    $switch = !$switch;    

}

function lightbulb($a)
{
    if ($a == true) { echo 'a is On <br/>';}
    else { echo 'a is Off <br/>';}
}

$a = true ;
echo 'At the begining, a is On, then.... <br/>';

bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
?>

特别是当他为了实现微优化而将代码提升到可读性较低的级别时!这不是我的答案,我其实想知道哪一个更好,为什么,请不要说像这样的回答。任何对你有用的。若你们想遵循正确的OO,那个么就创建类并传递该类的实例,而不是半打参数。对象在php5中通过ref传递,因此您可以根据需要对其进行更改。使用类型转换以确保传递正确的类。在我看来,你把注意力集中在了错误的事情上。。。。如果你想优化你的代码集中在数据库调用和磁盘r/w上。我试图编辑这个,但是你的编辑超过了我的。。。您不能缩进普通文本,否则所有内容都将显示为代码。谢谢,但现在我已将其修复。。看到了吗?嗯,所有的东西都还在代码块中,所以没有;)请填写此答案并删除多余的缩进。另见
As initial, the real 'a' is..1
After the Reference operator...(see the source code)
b is equal to: 11 and a equal to: 11
Now we back as initial... (see the source code) 
After we back : b is equal to: 1 and a equal to: 1
Wait, why b must follow a? ... 
..what about if we change b alone? (see the source code)
After we change b alone, b equal to: 23 and a equal to: 23
WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! 
to 'clear this, we use 'unset' function on a (see the source code)
Now, after unset,b is equal to: 66 and a equal to: 1
<?php

function bulbswitch(&$switch)
{
    $switch = !$switch;    

}

function lightbulb($a)
{
    if ($a == true) { echo 'a is On <br/>';}
    else { echo 'a is Off <br/>';}
}

$a = true ;
echo 'At the begining, a is On, then.... <br/>';

bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
?>
At the begining, a is On, then.... 
a is Off 
a is On 
a is Off 
a is On 
a is Off 
a is On