PHP 透過引用傳遞

WBOY
發布: 2024-08-29 12:48:07
原創
345 人瀏覽過

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學習者快速成長!