4

I need something like this.

var thing_<?php echo $php_var->n ?> = <?php echo '32' ?>

want to create something like this :

var thing_342 = '32'
5
  • What's the question? And why do you need something like that anyway? thing['342'] would usually be saner. Commented Nov 19, 2010 at 16:21
  • It looks like you solved your own problem... Could you elaborate a bit more on what you're going for here? Commented Nov 19, 2010 at 16:21
  • 3
    No, you don't need this misfeature. Period. Commented Nov 19, 2010 at 16:26
  • You should definitely mention your overall goal here. What you are trying to do is 99% likely to be the wrong way to achieve your goal. Commented Nov 19, 2010 at 16:27
  • ..and possibly subject to JS code injection Commented Nov 19, 2010 at 16:46

3 Answers 3

2

You need an object, and use it as an associative array/hash/whatever you are used to call it.

var thing = {};
thing[342] = '32';

or

var thing = {342: '32'};
Sign up to request clarification or add additional context in comments.

Comments

1

Suppose $php_var->n = 342

var data = <?php echo $php_var->n ?>;
eval("var thing_" + data + "=32;");
alert(thing_342); // output 32

Simple example:

var data = 342;
eval("var thing_" + data + "=32;");
alert(thing_342); // output 32

Comments

0
var container;
var var_name='lolo';
container[var_name]='he he he';
alert(container.lolo);

not exactly what you want, but I guess it is close enough.

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.