Jump to content

updating a variable "dynamically"


janroald

Recommended Posts

This one is kind of difficult to explain, but here it goes :

 

Say $_SESSION[a] = 'true'

 

Following url is of course invalid, but constructed to show my intention:

index.php?name_of_variable=$_SESSION[a]&value_to_set=false;

 

Is it possible to send a literal reference to a variable and then evaluate the reference so that i can update the variable without using its name?

Link to comment
https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/
Share on other sites

I'm trying to update session variables through ajax requests.

I want to send the "name" of the variable over url and the value to give it.

 

usual solution is to do a standard :

if($_GET[this]=='that') {

  $that = ...

} else if ...etc

 

But i'm just trying to research the possibility to create a reference to an actual variable based on a string that tells me the variable name.

Create a reference from a variable variable maybe...

 

Sound any less confusing at all? :-) 

 

An example that may help.

 

<?php

  session_start();
  $_SESSION['that'] = 'bar';
  echo $_SESSION['that'].'<br />';
  if (isset($_GET['this'])) { // pass a url like ?this=that&val=foo
    if (isset($_GET['val'])) {
      $_SESSION[$_GET['this']] = $_GET['val'];
    }
  }
  echo $_SESSION['that'];

?>

No, not that easy :-)

forget sessions, think just a simple variable

 

$foo = 'bar';

 

// pass a url like ?this=foo&value=foobar

 

How can i now, if at all possible, use $_GET[this] to create a reference to $foo and update it so that

 

echo $foo; //foobar

Better example of what i'm trying to do. (of course, this dont work)

// pass a url like ?this=foo&value=foobar

$foo = 1;

eval("\$bar =& \$$_GET[this];")

$bar = 2;

echo $foo; //2

 

Had a feeling this was a dead end, but had to try :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.