Jump to content

[php] how to make the name of the variable be the same as another variable?


michalchojno

Recommended Posts

How to make the name of the variable be a string of text, which you have in another variable?

 

Example:

$var = 'variable';

$variable = 'This is just some text';

 

Now I don't want to name it just $variable, but the name comes as a result of $var. How do I do that?

 

I tried this, which didn't work:

$var = 'variable';

('$'.$var) = 'This is just some text'; // doesn't work

 

Any tips?

Link to comment
Share on other sites

in the real world, if your variable data (and variable name) are changing, you'll echo it this way:

<?php
$var = "variable";
$$var = "some text";
echo $$var;

I don't know of a time where you will dynamically name a variable and then call a statically named variable.

Link to comment
Share on other sites

OK, your code works.

 

Now why this code doesn't?

$word = ('$'."$items5[$counter]".'5[$pid]');

$var5[$counter] = $$word;

echo "$var5[$counter]";

 

The result is empty.

 

EDIT: Perhaps the $ sign in variable $word? But I changed that and still no result.

 

In other words that would go:

 

$var = "variable[$counter]";

$$var = "some text";

echo $$var;

 

Would that work?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.