0

I'm a novice at php, and the upshot is that I am trying to have the character "$" treated as a variable designator for a variable defined by a combination of strings.

I am trying to generate a dynamic variable that will integrate a url-passed variable and into a new variable. In the example below, the url-passed variable ($url_var) will be numeric (1, 2, 3, etc.). This variable has been defined in php with $_REQUEST. Then I want to combine this variable with character strings to define a new variable ($title) that is dynamic and depends on the value of $url_var. There are multiple "titles" that are relevant (e.g., $pub1_title, $pub2_title), that related directly to $url_var. That is, $pub1_title would be relevant when $url_var = 1, and so on. So I can simply define $title for each case (e.g, $title=$pub1_title when $url_var =1), but I'd like to have a single dynamic line of code. To do this, I need to have the character "$" interpreted as a variable designator. Quite simply, how do I do this? And, is there a more efficient way to do this?

<? 
// Define Pub Variables
$url_var = $_REQUEST['url_var'];
$pub1_title = "pub1_title";
$pub2_title = "pub2_title";

// Make Title Variable

$title = "$" . "pub" . $url_var . "_title";  // Does not work (returns "$pub1_title")
//$title = $pub1_title;                      // This works, but is static.
echo $title;

?>
3
  • 1
    Read about variable variables. Commented Mar 19, 2018 at 20:45
  • ^ ... and then consider if that's really what you want to be doing. Maybe put your values into an array, db table, etc. Commented Mar 19, 2018 at 20:47
  • Possible duplicate of PHP and variable variables ($$) syntax Commented Mar 19, 2018 at 20:49

1 Answer 1

3

You're asking about variable variables.

And it's possible to do what you're trying to do, your syntax is just a bit off.

$title = ${"pub" . $url_var . "_title"};

To be honest, I don't really recommend using them, though. I've generally found them more confusing than useful, and prefer to use arrays instead. For example:

//array instead of separate variables
$pub_titles = [
    1 => "pub1_title",
    2 => "pub2_title"
];

// Refer to the array key here instead of using a dynamic variable
$title = $pub_titles[$url_var];
echo $title;
Sign up to request clarification or add additional context in comments.

3 Comments

That's it exactly.
I like the idea of using an array instead. This seems much cleaner to me.
I think there are a lot of advantages. You are dealing with a collection of similar things, and an array is a logical way of grouping them together. It gives you the option of easily doing things to the collection that you can't do with separate variables, sorting, filtering, etc. (Not that you need to do those things for this specific example, but just in general.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.