Giả sử giá trị sau:
$nextValue = 100;
Lấy biến mới và gán tham chiếu:
$currentValue = &$nextValue;
Để gán tham chiếu, dùng toán tử gán tham chiếu =&$anyVariableName trong PHP.
Ví dụ
<!DOCTYPE html> <html> <body> <?php $nextValue = 100; $currentValue = &$nextValue; echo "Next Value = ", $nextValue, "<br>"; echo "Current Value = ", $currentValue, "<br>"; $nextValue = 45000; echo "Sau khi thay đổi next sẽ phản ánh current vì =&", "<br>"; echo "Next Value = ", $nextValue, "<br>"; echo "Current Value = ", $currentValue; ?> </body> </html>
Kết quả
Next Value = 100 Current Value = 100 Sau khi thay đổi next sẽ phản ánh current vì =& Next Value = 45000 Current Value = 45000