สมมติว่ามีค่าดังนี้
$nextValue = 100;
ให้ตัวแปรใหม่รับอ้างอิง
$currentValue = &$nextValue;
เพื่อกำหนดค่าอ้างอิง ให้ใช้ตัวดำเนินการกำหนดค่าอ้างอิง คือ =&$anyVariableName ใน PHP
ตัวอย่างโค้ด
<!DOCTYPE html> <html> <body> <?php $nextValue = 100; $currentValue = &$nextValue; echo "Next Value=", $nextValue, "<br>"; echo "Current Value=", $currentValue, "<br>"; $nextValue = 45000; echo "After changing the value of next will reflect the current value because of =&", "<br>"; echo "Next Value=", $nextValue, "<br>"; echo "Current Value=", $currentValue; ?> </body> </html>
ผลลัพธ์
Next Value=100 Current Value=100 After changing the value of next will reflect the current value because of =& Next Value=45000 Current Value=45000