This article uses an example to analyze the problem of error when passing a reference in php5.4. Share it with everyone for your reference, the details are as follows:
In php5.3 series and previous versions, there is no problem in passing references. After upgrading to php5.4, all errors are reported when passing references
Fatal error: Call-time pass-by-reference has been removed in F:workwampwwwtesttest.php on line 6. Take a look at the example below.
Example 1, recursive reference transfer, test results in PHP 5.3 and above
<?php function test($aa,&$bb){ if($aa < $bb){ echo $bb."<br>"; $bb--; test($aa,&$bb); } } $aa = 3; $bb = 6; test($aa,&$bb); ?>
The running results are as follows
6
5
4
It won’t work after PHP 5.4. I tested it with PHP 5.4.11. ReportedFatal error: Call-time pass-by-reference has been removed in F:workwampwwwtesttest.php on line 6.
Example 2, tested on php5.4.11, only passing references
<?php function test($aa,&$bb){ if($aa < $bb){ $bb--; echo $bb."<br>"; //test($aa,&$bb); } } $aa = 3; $bb = 6; test($aa,$bb); //php5.4,php5.3及以前版本,输入结果是5 //test($aa,&$bb); //php5.3输入结果是5,php5.4及以后,报错Fatal error: Call-time pass-by-reference 。。。。 ?>
After php5.4, you can have & when defining. If you add & when calling, an error will be reported. It feels weird to call it this way.
Example 3, php5.4 and recursive reference
<?php function test($aa,&$bb){ if($aa < $bb){ $bb--; echo $bb."<br>"; test($aa,$bb); } } $aa = 3; $bb = 6; test($aa,$bb); ?>
php5.2, php5.3, and php5.4 can all be run. The results are:
5
4
3
I feel that upgrading from 5.3 to 5.4 is a bit cheating. If you don’t know the changes, upgrading will be very depressing.
Readers who are interested in more content related to PHP errors and exceptions can check out this site's special topic: "Summary of PHP error and exception handling methods"
I hope this article will be helpful to everyone in PHP programming.