Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Location
    India
    Posts
    109

    Unanswered: Dynamic Variables

    I have a problem in the below script where I try to create dynamic variables. Can anyone tell me what am I doing wrong here?

    PHP Code:
    PHP Code:
    function 
    fun1($arg)
    {

    $Var=$arg;
    global 
    $Var,$$Var;
    $
    $Var=1;

    fun2($arg);
        
    }

    function 
    fun2($arg)
    {

    global 
    $Var,$$Var;
    $Var=$arg;

    $
    $Var++;
    echo 
    $rstest;

    }


    $var="rstest";

    fun1($var); 

    I am trying to create $rstest variable and increment it in an other function. I guess this is not the way to do it. But do any of you people know how to create a dynamic variable and access it across other functions?

    In the above case, echo $rstest prints 1 whereas I expect 2.

    Thanks in advance
    Last edited by rexselin; 11-30-05 at 05:16.

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Not really sure what you are trying to do this for...
    Your global variables aren't really supposed to be global I don't think. Things just seem very very confusing...for starters:
    PHP Code:
    function fun1($arg)
    {

    $Var=$arg;            //assign variable $Var the value of $arg
    global $Var,$$Var;    //$Var now corresponds to the global variable $Var 
                    //(which doesn't exist --> you've defined $var)
                    //we also have a global variable variable $$Var now...
    $$Var=1;

    fun2($arg);
        

    I don't think you should be using globals here. Rather try using & to pass by reference. Try this and make your code a bit more readable and repost.

    Read: Pass By Reference

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •