Variable Variables

   

One thing that I've found incredibly useful in some of my form-based applications is variable variables. Variable variables allow you to have variable names for variables. This way you can generate variable names on the fly for whatever purposes your script requires.

Example:

 $name =  "integer";  $$name = 10; //results in $integer = 10; 

Note that when you echo these values to the screen, you get different results from those which you may expect. In the above example, if you want to print the value out to the screen, you cannot enclose the variable variable name in quotes as you might normally:

 echo "<p>$$name";  // results in "$integer" being printed to the screen echo "<p>" . $$name; //results in "10" being printed to the screen 

Variable variable names can also be created using expressions such as the following:

 $name = "integer";  ${$name . "_1"} = 10; // results in $integer_1 = 10; 

When coupled with a for loop, this helps create dynamic forms that can be easily changed to include more or fewer fields.

The example below, shown in Figure 4-2, allows you to upload files to a server. You just need to specify how many files you would like to upload, and the script automatically creates the necessary variables.

Script 4-5 upload.php

[View full width]

  1.  <?  2.  $page = "upload.php";  3.  ?>  4.  <html>  5.  <head>  6.    <title>Upload Files</title>  7.  </head>  8.  <body>  9.  <? 10. 11.  function specify_number() { 12.    global $page; 13.    ?> 14.    <form action=<?=$page?> method=post> 15.    Enter number of fields to display for upload and press Enter: <input type="text"  graphics/ccc.gifname="fields" size="2" maxlength="2"> 16.    </form> 17.    <? 18.  } 19. 20.  function print_form($fields) { 21.    global $page 22.    ?> 23.    <form action=<?=$page; ?> method=post enctype="multipart/form-data"> 24.    <table border=1> 25.    <? 26.    for($i = 0; $i < $fields; $i++) { 27.    ?> 28.    <tr><td>File:</td> 29.    <td> 30.     <input type="hidden" name="MAX_FILE_SIZE" value="1024000"> 31.     <input type="file" name="file_<?=$i?>"> 32.    </td></tr> 33.    <? 34.    } 35.    ?> 36.    </table> 37.     <input type="hidden" name="fields" value="<?=$fields?>"> 38.     <input type="submit" name="submit" value="Upload!"> 39.    </form> 40.    <? 41.  } // end print_form 42. 43.  $uploaddir = "/tmp/"; //make sure you include the trailing slash. 44.  if(isset($submit)) { 45.    for($i = 0; $i < $fields; $i++) { 46.      if(is_uploaded_file(${"file_".$i})) { 47.        $newfile = $uploaddir . $_FILES['file_' . $i]['name']; 48.        if(!@copy(${"file_".$i}, $newfile)) { 49.          echo "<p>Error Uploading <i>" . $_FILES['file_' . $i]['name'] . "</i>."; 50.        } else { 51.          echo "<p>Upload OK for " . $_FILES['file_' . $i]['name']; 52.        } 53.      } 54.      flush(); 55.    } 56.  } elseif(isset($fields)) { 57.    print_form($fields); 58.  } else { 59.    specify_number(); 60.  } 61.  ?> 62.  </body> 63.  </html> 
Figure 4-2. upload.php

graphics/04fig02.jpg

Script 4-5. upload.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Define the name of the page so that it can be used in functions that submit forms.

4 8

Print out the beginning HTML for the page.

11

Declare the specify_number() function. This function simply allows the users to enter the number of files that they wish to upload.

12

Allow this function to access the global $page variable.

14 16

Print the form that asks the users how many files they want to upload.

20

Declare the print_form() function. This function takes one argument, $fields, which is the number of fields the user entered into the specify_number function's form.

23

Create the form.

24

Create a table to display the form fields.

26 32

Create a form field for a file upload. Do this as many times as the user entered for the number of fields. Note how the field is named on line 31.

36 39

Close the table and the form. Add a hidden variable for the number of fields that were requested.

41

End the function.

43

Specify an upload directory for the files.

44

If the submit button has been pressed, then execute through line 56.

45

Create a for loop that goes through the same number of iterations as specified by the user for files to be uploaded.

46

Verify that a file has been uploaded by the user. Note the variable variable.

47

Create a variable that contains the path and filename for the uploaded file.

48

Attempt to copy the file from PHP's temporary file space to the location you have specified in the $newfile variable.

49

Notify the user if there is an error.

51

Notify the user if the upload was successful.

54

Flush the notification to the user.

56

If the submit button has not been pressed but the $fields variable has been set, then we know the user just entered how many files uploaded.

57

Execute the print_form() function, since the above statement is true.

58

If the submit button has not been pressed and $fields is not set…

59

Execute the specify_number() function.

60

End the if/else statement started on line 44.

62 63

Close out the HTML for the page.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net