PHP 通过引用传递

WBOY
发布: 2024-08-29 12:48:07
原创
346 人浏览过

PHP 编程语言本身的“通过引用传递”一词表示,每当变量通过引用传递时,就会在变量的参数之前添加与符号 (&)。现在以“function(&$x)”为例,这里全局变量和函数变量的范围都将成为全局值,因为它们是使用相同的引用概念定义的。因此,每当全局变量发生变化时,函数内部的变量也会发生变化,反之亦然,它将适用于所有这些。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法和参数

function function_name(&$function_parameter){
..
..
}
登录后复制

PHP传参的参数说明:

  • Function_name: function_name 参数只不过是用户定义的函数的名称。人们可以根据您的要求和任务命名任何内容。
  • Function_parameter: 这是 PHP 编程语言中用于按引用传递的变量参数。这个可变参数可以是任何东西。它也是用户定义的,可以根据您的编码要求命名为任何内容。但应该用&符号来表示。

PHP 中的引用传递如何工作?

PHP 编程语言的引用传递基本上只是在变量参数之前的与符号 (&) 的帮助下通过引用绕过变量。如果我们通过引用概念传递变量,那么函数就可以具有修改变量的能力。通过这种方式,PHP 通过引用传递的概念就起作用了,并且在某些编码案例中了解它是一个重要的概念。对于某些 PHP 版本,按引用传递的概念效果不佳。在 PHP 5.3.0 中,你会遇到一个警告,提示“call-time pass-by-reference”,而在 PHP 5.4.0 版本中,call-time pass-by-reference 将被删除,因此使用它会引发一些致命错误在某些 PHP 版本中。

通过引用实现 PHP 传递的示例

以下是提到的示例:

示例#1

这是说明 PHP 编程语言的引用传递概念的示例。这里首先创建一个函数“calculate()”及其引用函数参数“$a1”。然后在函数内部,该变量值会递增。然后,在函数“$a1”出来后,变量值被声明为“5”。然后通过使用 echo 功能将打印 $a1 的值,然后调用calculate() 函数。调用该函数后,再次使用 echo 函数,这次由于递增,变量值将打印为“6”。

代码:

<?php
function calculate(&$a1){
$a1++;
}
$a1=5;
echo "This is the value of a1 variable before the function call :: ";
echo $a1;
echo "<br>";
echo "This is the value of a1 variable after the function call :: ";
calculate($a1);
echo $a1;
?>
登录后复制

输出:

PHP 通过引用传递

示例#2

这也是说明 PHP 语言相同概念的示例,但这里使用字符串值,而不是像示例 1 那样使用数值。这里 print_string() 函数在 & 符号后面带有变量“string1”。然后在函数内部,将 string1 变量值分配为“Function Reason”,然后用于打印 string1 变量值。然后在函数之外,再次将 string1 变量值分配为“Globalake”。然后再次使用打印功能打印 $string1 变量,但这次打印的是函数内部存在的字符串值,而不是全局值。这是因为 PHP 的概念相同。

代码:

<?php
// Here Function is used just for assigning the new value to
// some $string1 variable and then printing it
echo "This is the exampe of PHP pass by reference concept :: ";
echo "<hr>";
function print_string( &$string1 ) {
$string1 = "Function sake \n";
// Print $string1 variable
print( $string1 );
echo "<br>";
}
// Drivers code
$string1 = "Global sake \n";
print_string( $string1 );
print( $string1 );
echo "<br>";
?>
登录后复制

输出:

PHP 通过引用传递

示例 #3

此示例与示例 2 类似,但这里不使用 & 符号只是为了确定如果不使用它会发生什么。如果不使用&符号,我们也可以将其称为按值传递概念。因此,如果您不具备这种能力,请不要忘记在函数的可变参数之前使用 & 符号。在第二个打印输出中,由于缺乏通过引用概念,您将得到“Globalake”。只需查看示例 2 和示例 3 的输出即可更好地理解这个概念。

代码:

<?php
// Here Function is used just for assigning the new value to
// some $string1 variable without using ampersand and then printing it
echo "This is the example of PHP pass by reference concept but exempted ampersand symbol :: ";
echo "<hr>";
function print_string( $string2 ) {
$string2 = "Function sake \n";
// Print $string1 variable
echo "This is the function's variable parameter value inside function :: ";
print( $string2 );
echo "<br>";
}
// Drivers code
$string2 = "Global sake \n";
print_string( $string2 );
echo "This is the function's variable parameter value outside the function :: ";
print( $string2 );
echo "<br>";
?>
登录后复制

输出:

PHP 通过引用传递

Example #4

This is the example of implementing the swapping functionality with the help of the swap() function and the same concept of the PHP Programming Language. Here at first two variables a1 , b1 is created with numerical and string values. Then those numbers are swapped with the swap() function. Then inside of the function using an extra variable value are jumbled to one other for swapping functionality. Then swapping will be done with that function. You can check out the output in the output section to understand the pass by reference with the swapping mechanism of PHP.

Code:

<?php
// ------------------------------------------
// This is sample Demo call of swap(...) function below.
echo "This is the example of swapping two variable values using the pass by reference concept of PHP :: ";
echo "<br>";
$a1 = 123.456;
$b1 = 'abcDEF';
print "<pre class="brush:php;toolbar:false">Define:\na1 = $a1\nb1 = '$b1'
"; swap($a1,$b1); print "
After swap(a1,b1):\na1 = '$a1'\nb1 = $b1
"; // ------------------------------- function swap (&$arg11, &$arg12) { // Now Swapping the contents of the indicated variables. $w1=$arg11;   $arg11=$arg12;   $arg12=$w1; } ?>
登录后复制

Output:

PHP 通过引用传递

Conclusion

I hope you learned what is the definition of PHP pass by reference along with its syntax and some explanation of the parameters, How the works in PHP Programming Language along with various examples of PHP to understand PHP pass by reference concept better.

以上是PHP 通过引用传递的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!