The Most Active and Friendliest
Affiliate Marketing Community Online!

“AdsEmpire”/  Direct Affiliate

Tip : static and global variables in php

D

dman_2007

Guest
Quick tip to let you know that assigning reference to a static variable or a global variable is not a good idea. The reason is because both static variables and access to global scope variables in local function by declaring them global is implemented using references. So, when you assign a reference to a variable declared global, it will just make it point to a new memory location instead of the original global variable. For example,

Code:
<?php
  function test_global()
  {
    global $test_var;
    
    $another_test_var = 12;

    echo 'function test_global : $test_var before reference assignment & value change ', $test_var, '<br />'; 
    $test_var = &$another_test_var;
    $test_var = 20; 
    echo 'function test_global : $test_var after reference assignment & value change ', $test_var, '<br />'; 
  }
  
  $test_var = 1;
  
  echo '$test_var before calling test_global function ', $test_var, '<br />'; 
  test_global();
  echo '$test_var after calling test_global function ', $test_var, '<br />'; 
?>

code given above produces the following output :

$test_var before calling test_global function 1
function test_global : $test_var before reference assignment & value change 1
function test_global : $test_var after reference assignment & value change 20
$test_var after calling test_global function 1

as you can see, $test_var does not points to global $test_var variable after reference assignment.

Similarly, when a static variable is assigned a reference to another variable, the value is not retained across the function call as it normally should. For example,

Code:
<?php
  function test_static()
  {
    static $funct_call_cnt = 1;
    
    echo 'Function call count : ', $funct_call_cnt++, '<br />'; 
    
    static $test_var;    
    $another_test_var = 12;
    
    if(isset($test_var))
    {
      echo '$test_var is set to ', $test_var, '<br />';
    }
    else
    {
      echo '$test_var is not set', '<br />';;
      $test_var = &$another_test_var;
      echo '$test_var is now set to ', $test_var, '<br />';
    }
     
  }
  
  test_static();
  echo '<br />';
  test_static();
?>

the code given above produces the following output :

Function call count : 1
$test_var is not set
$test_var is now set to 12

Function call count : 2
$test_var is not set
$test_var is now set to 12
 
this is bizarre...

accessing the var via the globals array seems to work
PHP:
<?php
  function test_global()
  {
    $another_test_var = 12;

    echo 'function test_global : $test_var before reference assignment & value change ', $GLOBALS['test_var'], '<br />'; 
    $GLOBALS['test_var'] = &$another_test_var;
    $GLOBALS['test_var'] = 20; 
    echo 'function test_global : $test_var after reference assignment & value change ', $GLOBALS['test_var'], '<br />'; 
  }
  
  $test_var = 1;
  
  echo '$test_var before calling test_global function ', $test_var, '<br />'; 
  test_global();
  echo '$test_var after calling test_global function ', $test_var, '<br />'; 
?>

outputs:
Code:
$test_var before calling test_global function 1
function test_global : $test_var before reference assignment & value change 1
function test_global : $test_var after reference assignment & value change 20
$test_var after calling test_global function 20

and the assign by reference here also works...
PHP:
<?PHP

function test_global() {
    $another_test_var = 12;

    echo 'function test_global : $test_var before reference assignment & value change ', $GLOBALS['test_var'], '<br />'; 
    $GLOBALS['test_var'] = &$another_test_var;
    $another_test_var = 20; 
    echo 'function test_global : $test_var after reference assignment & value change ', $GLOBALS['test_var'], '<br />'; 
}
  
    $test_var = 1;
  
    echo '$test_var before calling test_global function ', $test_var, '<br />'; 
    test_global();
    echo '$test_var after calling test_global function ', $test_var, '<br />'; 
?>
 
Last edited by a moderator:
MI
Back