Jump to content

How do I recall a variable variable in PHP?


langemarkdesign

Recommended Posts

Here is what I am doing. I am setting variable variables like this:

 

 

$ref_num = 5;

$num = 0;

 

while($num<$ref_num) {

$num++;

$to_name_ . $num = trim($_POST['to_name_$num']);

$to_email_ . $num = trim($_POST['to_email_$num']);

}

 

 

This works great, but the problem is, when I need to recall the variable I try to do so like this:

 

 

$ref_num = 5;

$num = 0;

 

while($num<$ref_num) {

$num++;

if($to_name_ . $num=="") {

echo "Ok";

}

 

 

PHP does not like the way I am recalling the variable, and it always comes up blank.

 

Any ideas how to do what I am trying to do?

 

The $num_ref is a dynamic, user entered number. And I am creating one text field for each num_ref. I then need to be able, dynamically to check each text field individually.

if( ${'to_name_' . $num} == "" ) {

 

Doesn't work... But that is certainly on the right track for what I am trying to do.

 

Any other suggestions?

 

Works on my PHP 5.3 development machine. What version are you using?

 

Other suggestion: use an array instead.

5.2.13

 

I tried setting the variable like so...

 

$to_name_ . $num = "Fred";

 

Just to make sure it was setting correctly. And then recall it like so...

 

die("Name: " . ${'to_name_' . $num});

 

I also tried it as an if statement...

 

if(${'to_name_' . $num}=="") {
die("Blank");
}

 

Bu I am not having any luck.

This syntax you are using to 'set' the variables does not work in any version of php - $to_name_ . $num = "Fred";

 

You should just use HTML array variables for your form fields - http://www.php.net/manual/en/faq.html.php#faq.html.arrays You can then just use php's array functions (like a foreach() loop) to iterate over the data.

 

Using variable variables is three times slower than using an array. Also using variable variables is taking twice the memory, you are processing the data an extra time just to produce the variables, and the code is more complicated.

This syntax you are using to 'set' the variables does not work in any version of php - $to_name_ . $num = "Fred";

 

This does not appear to be true.

 

This code:

$num = 1;
$to_name_ . $num = "Fred";

 

Followed by this code:

die("Name: " . $to_name_1);

 

Returns the output, "Name: Fred".

 

So, it does set my variable variable correctly. My issue is retrieving the value, when I don't know what $num is.

Have to apologize to you 182guy... You had the solution all along.

 

This...

 

${'to_name_' . $num}

 

Recalls the correct variable variable, but only if you set it like this...

 

${'to_name_' . $num} = "Fred"

 

Not the way I was doing it like this...

 

$to_name_ . $num

 

Thank you so much to everyone for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.