Any way to use a var within a predefined var?

This is a discussion on "Any way to use a var within a predefined var?" within the PHP Forum section. This forum, and the thread "Any way to use a var within a predefined var? are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old May 8th, 2007, 22:56
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 245
Thanks: 6
Thanked 0 Times in 0 Posts
Any way to use a var within a predefined var?

This doesn't work: $_SESSION[$var]

Is there some workaround that does. I've looked into $$vars (man, those were tough to wrap my brain a round at first!) but I don't see how they could help me. I also tried wrapping the inner var in curly brackets; that didn't work either.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 10th, 2007, 16:46
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

I'm not quite sure what you're getting at. Maybe this will get you started.
Page 1:
PHP: Select all

    $multi=array();
    
$multi[0]="This is the first multi";
    
$multi[1]=17;
    
$_SESSION['multi']=$multi
Page Two:
PHP: Select all

$multi=$_SESSION['multi'];
echo 
$multi[1]; 
This works like a charm. You can use an associative array. It retains the cast of the variable values and everything.

On which page do you want to set the variable value? If I'm following you at all, you should be able to get the job done by assigning values first and putting them into the superglobal only for transmission.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 10th, 2007, 17:49
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 245
Thanks: 6
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

What I'm getting at is that I want to pass a variable name (and only the variable name) to a function and have it echo back the variable name and the variable's set value...
Code: Select all
function echoVar(var) {
echo "var=$var";
}
I'd add some string parsing to that function so I could do things like
Code: Select all
// show session var 'return2'
echoVar(SV_return2);
//returns: "$_SESSION['return2'] is set to http://example.com/origination.php"

// show post var 'address'
 echoVar(P_address);
//returns: "$_POST['address'] is set to 1234 Main Street"

// show get var 'sid'
  echoVar(G_sid);
 //returns: "$_GET['sid'] is set to fde58b062cb8391a06df3"
 
 // show Cookie var 'userID'
  echoVar(C_userid)
 //returns: "$_COOKIE['userid'] is set to 1185"
but I don't know how to do that without something like $_SESSION['$var']. It's just a debugging function, but it'd sure be helpful.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old May 10th, 2007, 21:15
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

I think I see what you want.

You could almost surely do this more elegantly using variable variables, but I always pick the easy way. I'm not sure a $$ approach would actually save any coding space.
PHP: Select all

function echoVar($type,$name) { 
 if (
$type=='S') { $x='$_SESSION'$value=$_SESSION[$name];
 }elseif (
$type=='C') { $x='$_COOKIE'$value=$_COOKIE[$name];
 }elseif (
$type=='P') { $x='$_POST';$value=$_POST[$name];
 }elseif (
$type=='G') { $x='$_GET';$value=$_GET[$name];
 }else{
     echo 
"<br />Improper first argument in EchoVar() for '$name', use S C P or G. <br />";
     return;
 }
   echo 
'<br />';
   if(
$value) {
   echo 
$x '[\'' $name '\'] is set to value ';
   echo 
$value;
   echo 
'.<br />';
   }else{
      echo 
$x '[\'' $name '\'] does not have a value set.';
   }

The only trick is not to get stuck trying to express a value for a superglobal inside double quotes. If you have to have EchoVar(SV_username) instead of EchoVar(S, username), my first thought would be to pop a substring replace in the first line or more likely inside the four if's.

Last edited by masonbarge; May 10th, 2007 at 21:21. Reason: Tidy up
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old May 10th, 2007, 21:35
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

While I'm at it, if my first try didn't do the trick, this snippet may help you figure out what you want to do. It is really two different approaches.
PHP: Select all

foreach ($_SESSION as $k => $v) {
     echo 
'<br />$_SESSION[' $k .'] ';
     echo 
"is set to $v. <br />";
     global $
$k;
     echo 
"$$k has a value of $v. <br />";
 } 
I use something like this sometimes with an intro "The following session variables are set".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old May 11th, 2007, 03:56
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 245
Thanks: 6
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

Awesome stuff, Mason. Thanks! I'll definitely get use out of that second example, too. I've already created functions ShowPost(), ShowGet() and ShowCookies() based on that code. Great debugging tools!

One quick question regarding the first code you posted...
I "made it my own" as follows:
Code: Select all
function echoVar($type,$name) { 
    if ($type=='S') {
         $var='$_SESSION[\'' . $name . '\']';
        $val=$_SESSION[$name];
    } elseif ($type=='C') {
        $var='$_COOKIE[\'' . $name . '\']';
        $val=$_COOKIE[$name];
    } elseif ($type=='P') {
        $var='$_POST[\'' . $name . '\']';
        $val=$_POST[$name];
    } elseif ($type=='G') {
        $var='$_GET[\'' . $name . '\']';
        $val=$_GET[$name];
    } elseif ($type=='N') {
        $var='(basic var) $'. $name;
        $val=$name;
    } else {
        echo "<br />Improper first argument in EchoVar() for '$name', use<br>S (SESSION), C (COOKIE), P (POST), G (GET) or N (none = plain var). <br />";
        return;
    }
    if($val) {
        echo $var.' is set to '.$val.'<br />';
    }else{
        echo $var . ' does not have a value set.<br />';
    }
}
Notice the "type=N" section? That doesn't work. I tried using $$name (I really don't get variable variables!) but that doesn't work either. What can I use there that would work?
Thanks so much for the help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old May 11th, 2007, 14:00
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Any way to use a var within a predefined var?

$$'s -- I sympathize. It helps me to fixate on the fact that I'm naming a variable, not setting a value, then remember that when I see or use one I have to expect to take two mental steps, not one. If it really bugs you, just try setting all your variables like this for a day or two.
PHP: Select all

$grimx='var';
$
$grimx=round($result2); or whatever 
EchoVar won't work for a plain variable because you can't enter both a variable name and a variable value into the $name argument. You are just going to get "username has a value of username" or "Bob has a value of Bob" depending on whether you enter "username" or "$username" as the argument.

Using $$ is just going to rename the variable to the value of $name. It's the wrong direction. I can't think of an easy fix. If someone knows how to do this, please chime in.

You can add an optional third optional argument $var which is going to get rather silly -- you'd be using it like:
PHP: Select all

echoVar(Nusername$username); 

or you could simply make another function. IIRC you can't conditionally define functions with the same name - the "if" clauses have to go inside the function definition. So you can't define EchoVar two different ways depending on whether the variable is an array or a simple.

If you want a more complex function definition, you might be able to set up the function you want by using variable parameters. I've never done it so I don't know how to do it or whether it would even get you where you want to go. It looks like kind of a pain but it would be fun.

Last edited by masonbarge; May 11th, 2007 at 14:08.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
predefined variables, variables

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 13:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42