PHP Spotting References

PHPServer Side ProgrammingProgramming

Introduction

Many syntax constructs in PHP are implemented via referencing mechanisms. If reference to a global variable is unset in a function, the same variable in global namespace is not removed.

Example

 Live Demo

<?php
$var1 = 'Hello World';
function myfunction(){
   global $var1;
   $var2 =&$var1;
   echo "$var1, $var2 \n";
   $var2="Hello PHP";
   echo "$var1, $var2 \n";
   unset($var1);
}
myfunction();
echo "$var1\n";
?>

Output

Global $va1 is intact.

Hello World, Hello World
Hello PHP, Hello PHP
Hello PHP

debug_zval_dump() function can be used if a variable has references to other variables

raja
Published on 18-Sep-2020 12:17:24
Advertisements