Function : setVar

The setVar is a complex function that is used to perform several functions which include

  • initialization of variable through reference
  • returning custom values for empty or undefined variables
  • modifying value of empty or undefined variables with custom values

Initializing variables by reference
  <?php

  setVar($name); 
  
  echo $name; //null
                  


Returning custom defined values for empty variables
  <?php 

  $return = setVar($name, 'foo');

  echo $return; //foo
                  
  <?php 

  $name = 'bar';

  $return = setVar($name, 'foo');

  echo $return; //bar
                  
In the first sample above, the value of 'foo' is set as a return custom value for $name if it is empty or it is not previously defined. However, sample 2 shows that if $name was previously defined, its relative value will be returned rather than the alternate value.

Modifying empty or undefined variable's value with custom value
  <?php 

  $alt = 'bar';

  setVar($value, $alt, true);

  echo $value; //bar
                  
In the sample above, $alt was predefined but $value was not predefined. By setting the third parameter as true, $alt value will overide $value because $value is empty or not previously defined. However, in the sample below, $value was previously defined which means that $alt will not be able to modify the value of $value.
  <?php 

  $value = 'foo';

  $alt   = 'bar';

  $return = setVar($value, $alt, true);

  echo $value; //foo

  echo $return; //foo