php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16654 A special question related to variable variables
Submitted: 2002-04-17 04:47 UTC Modified: 2002-04-17 06:16 UTC
From: liquid at corroded dot de Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.1.2 OS: Windows 2k
Private report: No CVE-ID: None
 [2002-04-17 04:47 UTC] liquid at corroded dot de
You will find the question on the bottom of this message!

The situation:
Let's say that I have 2 or more forms and want to store the posted values in a php-include-file. This might look like this:
1. Display the 1st form. The user enters some values and submits the form.
2. The script now stores the values in a file which can later be included by PHP.
3. Display the 2nd form with 2 submit buttons (back and next). The user enters values again, then presses the back button.
4. 1st we include the file we have stored in "2", then display the form, filled out with the values from the included file.

Ok, now the problem in detail:
The used forms may vary, each time the program executed, so the algorythm for storing the values needs to be kind of flexible. That's the count of values to store is diffenrent for each of the forms.
To make it easier, I use similar names for the variables names (var_1, var_2 ... var_n) in all of the forms.

Some code for further explanation:
(I'm using old style parameters to make it simpler here.)
<pre>
// File form1.inc.php:
&lt;?php
include("functions.inc.php");
storeValues($prevpage);
include("values1.inc.php");
?>

&lt;form action="index.php" method="post">
&lt;input type="text" name="prevpage" value="1" />

value1: &lt;input type="text" name="var_1" value="<?php echo $var_1; ?>" size="30" maxlength="30" /><br />

value2: &lt;input type="text" name="var_2" value="<?php echo $var_2; ?>" size="30" maxlength="30" /><br />

...

&lt;input type="submit" value="Next">
&lt;/form>
// end File

// File form2.inc.php:
&lt;?php
include("functions.inc.php");
storeValues($prevpage);
include("values2.inc.php");
?>

&lt;form action="index.php" method="post">
&lt;input type="text" name="prevpage" value="2" />

value1: &lt;input type="text" name="var_1" value="<?php echo $var_1; ?>" size="30" maxlength="30" /><br />

value2: &lt;input type="text" name="var_2" value="<?php echo $var_2; ?>" size="30" maxlength="30" /><br />

...

&lt;input type="submit" value="Back" name="back">
&lt;input type="submit" value="Next" name="next">
&lt;/form>
// end File

// File functions.inc.php:
&lt;?php
function storeValues($page)
{
  $fp = "values$page.inc.php";
  if(file_exists($fp)){
    $fp_new = "values$page.inc.new";
    if($fh = fopen($fp, "r")){
      if($fh_new = fopen($fp_new, "w")){
        while(!feof($fh)){
          $ln = fgets($fh, 1024);
          // HERE COMES THE TRICK!!
          if(ereg("var_([0-9]{1,2})", $ln, $var)){
            $vvar = "var_{$var[1]}"; // variable variable
            global $$vvar; // globalize $var_x from previously submitted form
            $tmp = "$";
            $tmp .= "$vvar = ${$vvar};\n";
          }
          fputs($fh_new, $ln);
        }
        fclose($fh_new);
      }
      fclose($fh);
    }
    if(file_exists($fp_new)){
      unlink($fp);
      rename($fp_new, $fp);
    }
  }
}
?>
// end File

// File values1.inc.php or values2.inc.php ...
&lt;?php
$val_1 = "xyz";
$val_2 = "zyx";
...
?>
// end File

// File index.php
&lt;?php
// find the button used to submit
if(isset($next)) // button was "next"
  $page = $prevpage + 1;
else if(isset($back)) // button was "back"
  $page = $prevpage - 1;
else
  $page = 1;

include("form$page.inc.php");
?>
// end File
</pre>

I hope you find the source code useful, because its not the original code I'm working on. Well it's quite similar.


Summary:
I have a defined variable and set a variable variable to the same name as the defined one, then I'm globalizing it.


The question i want to ask, is about the function "storeValues". Please have a look at it and tell me if the "TRICK" I'm using is ok with PHP?

I want to make sure that it will still work with future versions of PHP.

It looks a bit weird to me... But ok, it's working well with PHP 4.0.6
I did not find any useful documentation about it, so I needed to ask that here.


c ya
Liquid

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-17 06:16 UTC] sander@php.net
The bug system is not the appropriate forum for asking support
questions. For a list of a range of more appropriate places to ask
for help using PHP, please visit http://www.php.net/support.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 09:01:27 2024 UTC