[Solved] PHP: Variable variables.

  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

Hi,

I have a question about variable variables in PHP.

Can you do this:

Code: [ Select ]
$model$car = 15


I want the code to create a variable like: $fordcar or $nissancar, depending on the input. How would I achieve something like this?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

You could use something like:
PHP Code: [ Select ]
$model['ford'] = "15";

Then just replace ford with all the models of cars you want.
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

I'd suggest you do this:

PHP Code: [ Select ]
 
<?
 
$model = array();
 
$car = "whatever";
 
$model[$car] = 15;
 
 
 
echo $model["whatever"] //outputs 15
 
?>
 
 
  1.  
  2. <?
  3.  
  4. $model = array();
  5.  
  6. $car = "whatever";
  7.  
  8. $model[$car] = 15;
  9.  
  10.  
  11.  
  12. echo $model["whatever"] //outputs 15
  13.  
  14. ?>
  15.  
  16.  
  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

CazpianXI wrote:
I'd suggest you do this:

PHP Code: [ Select ]
<?
$model = array();
$car = "whatever";
$model[$car] = 15;
 
echo $model["whatever"] //outputs 15
?>
 
  1. <?
  2. $model = array();
  3. $car = "whatever";
  4. $model[$car] = 15;
  5.  
  6. echo $model["whatever"] //outputs 15
  7. ?>
  8.  


Can $car be a string in this example? If so, I think his way will be my best bet.
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Yes, $car can be a string. However, it cannot have any characters that would be invalid in a normal variable. (Just like you can't have a variable named "$bad var".

Other than that, the answer to your question to your question is yes.
  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

Thanks a lot. :)

I have this so far. This is valid, right?

Code: [ Select ]
<?php

$dg_name = array ();
$dg_name[$dg_by_quantity] = 3;
$dg_name[$dg_quantity] = 5;
$dg_name[$dg_price] = 10;
$ur_money = 100;

echo $dg_name[$dg_by_quantity];

?>
  1. <?php
  2. $dg_name = array ();
  3. $dg_name[$dg_by_quantity] = 3;
  4. $dg_name[$dg_quantity] = 5;
  5. $dg_name[$dg_price] = 10;
  6. $ur_money = 100;
  7. echo $dg_name[$dg_by_quantity];
  8. ?>
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Yes, the code you posted is valid.

I'm assuming, of course, that $dg_by_quantity, $dg_quantity, and $dg_price are defined earlier in the script?

Moses08 wrote:
Thanks a lot.

Glad to be of service!
  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

CazpianXI wrote:

I'm assuming, of course, that $dg_by_quantity, $dg_quantity, and $dg_price are defined earlier in the script?



They will be entered by a form.
  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

It just got more complex:

Code: [ Select ]
<?php

$dg_name = "Test";

$$dg_name = array ();
${$dg_name[$dg_by_quantity]} = 3;
${$dg_name[$dg_quantity]} = 5;
${$dg_name[$dg_price]} = 4;
$ur_money = 100;


echo $dg_name;
echo ${$dg_name[$dg_quantity]};


?>
  1. <?php
  2. $dg_name = "Test";
  3. $$dg_name = array ();
  4. ${$dg_name[$dg_by_quantity]} = 3;
  5. ${$dg_name[$dg_quantity]} = 5;
  6. ${$dg_name[$dg_price]} = 4;
  7. $ur_money = 100;
  8. echo $dg_name;
  9. echo ${$dg_name[$dg_quantity]};
  10. ?>


This isn't working properly. For some reason, echo ${$dg_name[$dg_quantity]}; is outputting the value of ${$dg_name[$dg_price]}. Any ideas?
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

What are you setting the values of $dg_quantity and $dg_price to?

Here's a debugging tip. Run this snippet of code after you defined all your variables:

PHP Code: [ Select ]
 
print_r($$dg_name);
 
 
  1.  
  2. print_r($$dg_name);
  3.  
  4.  


This will dump all the values of your array to the screen. If you can't make sense of it, paste it here and I'll explain it to you.
  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

The Code:

Code: [ Select ]
<?php

$dg_name = "Test";

$$dg_name = array ($dg_by_quantity, $dg_quantity, $dg_price);
${$dg_name[$dg_by_quantity]} = 3;
${$dg_name[$dg_quantity]} = 5;
${$dg_name[$dg_price]} = 4;
$ur_money = 100;

print_r($$dg_name);

?>
  1. <?php
  2. $dg_name = "Test";
  3. $$dg_name = array ($dg_by_quantity, $dg_quantity, $dg_price);
  4. ${$dg_name[$dg_by_quantity]} = 3;
  5. ${$dg_name[$dg_quantity]} = 5;
  6. ${$dg_name[$dg_price]} = 4;
  7. $ur_money = 100;
  8. print_r($$dg_name);
  9. ?>


The Output:
Code: [ Select ]
Array ( [0] => [1] => [2] => )


I want the script in the end to do this:

1. User fills out a form with 2 fields: $dg_name and $dg_by_quantity.

2. The form is processed with this script:
Code: [ Select ]
$ur_money = $ur_money - ${$dg_name}[$dg_price] * ${$dg_name}[dg_by_quantity]

${$dg_name}[$dg_quantity] = ${$dg_name}[$dg_quantity] + ${$dg_name}[$dg_by_quantity]
  1. $ur_money = $ur_money - ${$dg_name}[$dg_price] * ${$dg_name}[dg_by_quantity]
  2. ${$dg_name}[$dg_quantity] = ${$dg_name}[$dg_quantity] + ${$dg_name}[$dg_by_quantity]


Any ideas on how I can accomplish this?

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 132 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.