English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writing a routine for a point of sale system in a shop. They have a fairly simple and short stock list which fits on one screen. The user selects how many of an item they wish to sell and a JavaScript routine calculates the totals. I then POST this form to another PHP program where it needs to recover the information to print a receipt.

The problem is the JavaScript routine has some unusual PHP variable names which my program is auto building based on a flat file database.

To cut a long story short let's say one of my variables is called $PROD_KAY001_499 and it contains the value 2 which is the quantity the user entered.

If I say: echo $PROD_KAY001_499; it shows 2. No problem.

But what if I need to build that variable? Let's say I just did that and now $temp=$PROD_KAY001_499.

If I say: echo $temp; it shows '$PROD_KAY001_499' but I want to show the CONTENT of that variable, not the actual variable name itself.

Sorry if that was not all that clear, it's hard to explain...

2007-04-21 15:38:31 · 5 answers · asked by ZCT 7 in Computers & Internet Programming & Design

I am importing all variables from my other program with @import_request_variables.

Like I said if I manually plug the variable name into an echo statement it shows just fine. The problem is I am trying to take the variable name out of $temp.

So $temp contains the name of my variable, I just want to know the contents of the variable defined in $temp.

2007-04-21 17:05:04 · update #1

dhvrm,

Thanks, but I am using a routine to calculate the variable in question.

So for example:

$temp = '$PROD_'.$anothervariable.'_005';

So now I know what the variable name is the content of $temp.

But I want to find the content of $PROD_etc etc.

Again, sorry for the bad explanation of this problem.

2007-04-21 18:32:00 · update #2

5 answers

If what you mean is that for some reason, you want to dynamically declare variable names from strings stored in another variable, you can use double-dollar signs:

$temp = PROD_KAY001_499;
$$temp = 2;
echo ${$temp};
echo "
";
echo $PROD_KAY001_499;

It has been my general experience that stronger design -- especially by using classes -- almost always removes the requirement to dynamically declare variable names.

UPDATE:

My previous answer still holds true.

$temp2 = "FOOBAR";

$temp = "PROD_";
$temp .= $temp2;
$temp .= "_001";

$$temp = 2;

assigns 2 as the value of $PROD_FOOBAR_001

echo ${$temp}; // will echo 2

If you need to know the name of the variable:

print_r(${$temp});

will echo the name of the variable and its value.

Again, I cannot overemphasize that the way you are proceeding should be rethought. It seems, from your problems explaining your issue and the snippets you have provided, that your design is bad.

2007-04-21 18:23:11 · answer #1 · answered by Anonymous · 1 0

[quote]
$temp=$PROD_KAY001_499.
[/quote]

are you sure that this statement does not contain any quotes (single or double), otherwise I cannot reproduce the error.

$temp=$PROD_KAY001_499;
should assign the value of the right hand side variable to the left hand side one.

unless you do
$temp='$PROD_KAY001_499';

where the right hand side is a string rather than a variable

2007-04-21 20:46:43 · answer #2 · answered by my_screen_name 2 · 0 0

echo $$temp

2007-04-22 05:28:07 · answer #3 · answered by SynchoAsyncher 1 · 0 0

you will desire to positioned some javascript code (function) in the Hypertext Preprocessor document and make contact with your base javascript applications (that are in a seperate .js document) from that function. i might recommend to place the javascript code on the tip (footer section) of the Hypertext Preprocessor document. yet in some tutorials it says this is a private determination, and a few locate it greater handy to place the js code in header. i wish this permits.

2016-12-10 08:16:33 · answer #4 · answered by ? 4 · 0 0

are you sure both variables are within the same scope ?
are you sure that PROD_KAY001_499 is being created before temp ?

2007-04-21 16:26:30 · answer #5 · answered by dave_h4 2 · 0 1

fedest.com, questions and answers