PHP Spotting References


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 
";    $var2="Hello PHP";    echo "$var1, $var2
";    unset($var1); } myfunction(); echo "$var1
"; ?>

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

Updated on: 18-Sep-2020

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements