简体   繁体   English

PHP参考通行证

[英]php reference pass

I have a problem with passing variables as reference in PHP. 我在PHP中将变量作为引用传递时遇到问题。

I want to set $grand_total after: 我想在以下位置设置$grand_total

$data = array('title' => 'blabla', 'value' => &$grand_total);

// Set after
$grand_total += 50;

This is running without a problem, but when I pass $grand_total var using a function, I get the error below: 这运行没有问题,但是当我使用函数传递$grand_total var时,出现以下错误:

PHP Error was encountered Severity: 8192 Message: Call-time pass-by-reference has been deprecated Filename: controllers/checkout.php Line Number: 131 遇到PHP错误严重性:8192消息:已不赞成调用时传递引用文件名:controllers / checkout.php行号:131

I'm passing the variable like this: 我正在像这样传递变量:

$data = array('title' => 'blabla', 'value' => price(&$grand_total));

// Set after
$grand_total += 50;

I must use the price() function. 我必须使用price()函数。

Can anyone help me? 谁能帮我?

I'm sorry for my bad English. 对不起,我的英语不好。

This would work perfectly fine for you 这对你来说很好

    $grand_total = 10;
    function &price($value)
    {
        $value += 10;
        return $value ;
    }
    $grand_total = price($grand_total);
    $data = array('title' => 'blabla', 'value' =>$grand_total);
    $grand_total += 50;
    var_dump($grand_total)

Output 产量

    int 70

For more Information 欲获得更多信息

http://us3.php.net/manual/en/language.references.php http://us3.php.net/manual/zh/language.references.php

http://us3.php.net/manual/en/language.references.pass.php http://us3.php.net/manual/zh/language.references.pass.php

Thanks :) 谢谢 :)

Without the function its a bit difficult. 没有该功能,将有些困难。 We don't know what is on line "131" 我们不知道“ 131”行上的内容

Look to your php.ini 查看您的php.ini

allow_call_time_pass_reference = On

If this parameter is set to off its not working. 如果将此参数设置为关闭,则它不起作用。 But general its deprecated in PHP 5 to call the time function with a reference ("&$grand_total"). 但是一般而言,PHP 5中已弃用使用引用(“&$ grand_total”)调用time函数。 Call the function without the reference or assign the reference in your function to another variable and use the variable not the reference. 调用不带引用的函数,或将函数中的引用分配给另一个变量,然后使用变量而不是引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM