PHP
  Home arrow PHP arrow Page 3 - Using Variable Variables in PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using Variable Variables in PHP
By: Eric Seufert
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2001-02-14

    Table of Contents:
  • Using Variable Variables in PHP
  • Variable Variables?
  • Using Variable Variables with Arrays
  • Variable Variables with Functions
  • Variable Variables with Classes
  • The Never-ending Variable
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Variable Variables in PHP - Using Variable Variables with Arrays


    (Page 3 of 7 )

    Variable Variables make working with forms and other such user-input easier because they allow you to centralize all the available options. Getting back to my aforementioned problem, I had to figure out a way to select only the stories with the criteria that the person selected. The easiest way to go about this was to create an array to hold only the stories that fit the criteria the user specified and then create a MySQL query string with the values. Here’s the code I used:

    $atts = array('comedy', 'tragedy', 'horror', 'adventure', 'poetry', 'screenplay', 'satire'); $vals = array(); for ($i = 0; $i < sizeof($atts); $i++) { if (${$atts[$i]} == 1) { $last = sizeof($vals); $vals[$last] = "$atts[$i]"; } } $query_build = ""; for ($i = 0; $i < sizeof($vals); $i++) { $curval = "$vals[$i] = '1'"; if ($i != (sizeof($vals) - 1)) { $query_build .= $curval." OR "; } else { $query_build .= $curval; } }

    (*note* The system running the program did not have PHP 4.0 installed at the time, which is why I did not just use array_push to add values to the array in the first loop)

    The actual values of the variable variables created from the $atts array having been already passed to the program, everything was accomplished in a few lines of code rather than replicating the same process for each different characteristic, showing the efficiency of variable variables when used with forms.

    Building arrays from variable variables is equally simple. If, perhaps, I wanted to include my middle and last name within the $$x variable, I could initialize it as an array with the following code:

    $$x = array("Benjamin", "Seufert");

    However, array elements cannot be directly accessed in variable variable form, so outputting my entire name could be handled in one of two ways:

    $temp1 = ${$x}[0]; $temp2 = ${$x}[1]; echo "$x $temp1 $temp2";

    or,

    echo "$x $Eric[0] $Eric[1]";

    both of which would print,

    Eric Benjamin Seufert

    Had I tried to directly print the variable variable array elements, I would have received the following output:

    Eric Array[0] Array[1]


    More PHP Articles
    More By Eric Seufert


     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT