6

Depending on the current page, I would like to change the css of the chosen menu module and make others different, etc. All that while building dynamically the page before loading.

My problem is that I have a serie of variables going by this structure :

$css_(NAME_OF_MODULE)

To know what variable must be set , I have another variable I received in parameters of this functions, called

$chosen_menu

Say $chosen_Menu = "C1", I would like to add content to $css_C1. Thus, what I want is to create a variable name out of 2 variables, named $css_C1

I have tried :

${$css_.$chosen_menu} = "value";

But it doesnt seem to work. Any clue ?

3
  • Why do you use $css_ instead of $css? Commented Nov 13, 2011 at 15:28
  • Why not ? Is there any reason not to ? EDIT : There was an error in the question. Commented Nov 13, 2011 at 15:29
  • 6
    Why don't you just use an associative array? Commented Nov 13, 2011 at 15:31

4 Answers 4

5

That probably won't just work. PHP supports full indirection though, so something like this will work.

$varName = $css."_".$chosen_menu;
$$varName = "value";

If not, it will probably be attempting to interpret $css_ as a variable name in your second code sample, so just change that to $css."_".$chosen_menu.

Sign up to request clarification or add additional context in comments.

Comments

4
$nr = 1;        
       ${'name' . $nr} = 20 ;        
       var_dump($name1);   

Comments

2

Check out http://php.net/manual/en/language.variables.php

You should be able to use:

$menu = $css . '_' .$chosen_menu;
$$menu = 'some value';

or

${$menu} = 'some value';

Comments

0

I'm not sure if I get you right, but how about this:

${$css."_".$chosen_menu} = "value";

Comments

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.