langemarkdesign Posted April 5, 2010 Share Posted April 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/ Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 You can do that like this: if( ${'to_name_' . $num} == "" ) { I think that's what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037458 Share on other sites More sharing options...
langemarkdesign Posted April 5, 2010 Author Share Posted April 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037473 Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037479 Share on other sites More sharing options...
langemarkdesign Posted April 5, 2010 Author Share Posted April 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037482 Share on other sites More sharing options...
PFMaBiSmAd Posted April 6, 2010 Share Posted April 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037522 Share on other sites More sharing options...
langemarkdesign Posted April 6, 2010 Author Share Posted April 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037902 Share on other sites More sharing options...
langemarkdesign Posted April 6, 2010 Author Share Posted April 6, 2010 Although PFMaBiSmAd, your suggestion is quite valid... and would have saved me a lot of headache. I still would like to try to get down to my original solution however, just to know. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037904 Share on other sites More sharing options...
langemarkdesign Posted April 6, 2010 Author Share Posted April 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197686-how-do-i-recall-a-variable-variable-in-php/#findComment-1037912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.