Reference assignment operator in PHP to assign a reference?



Let’s say we have the following value −

$nextValue=100;

Let us take a new variable and assign a reference −

$currentValue = &$nextValue;

To assign a reference, use the reference assignment operator i.e. =&$anyVariableName in PHP.

The PHP code is as follows −

Example

 Live Demo

<!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>

Output

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
Updated on: 2020-10-12T13:41:24+05:30

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements