473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

PHP: Using variable variables.

Markus
6,050 Expert 4TB
Using the code below you can turn array indices into variables that can be accessed as a normal variable would be.



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3. * @author      Mark Skilbeck - MAHCUZ.COM
  4. * @title    PHP: Variable variables.
  5. **/
  6.  
  7. $array = array("Name" => "Foo", "Age" => 21);
  8.  
  9. foreach( $array AS $Key => $Val )
  10. {
  11.     $var   = $Key;
  12.     $$var  = $Val;
  13. }
  14.  
  15. // the array can now be accessed
  16. // by using:
  17. //
  18. // echo $array_key;
  19.  
  20. echo "Name: $Name - Age: $Age";
  21. // prints Name: Foo - Age: 21
  22. ?>
  23.  
Apr 28 '08 #1
3 5165
Markus
6,050 Expert 4TB
Just found out you can use the extract() function instead.

My work is pointless!
Apr 29 '08 #2
ronverdonk
4,258 Expert 4TB
Just found out you can use the extract() function instead.
My work is pointless!
No it is not. At leastt you showed how you can use and manipulate dynamic variable names.

Ronald
May 5 '08 #3
hsriat
1,654 Expert 1GB
At least you showed how you can use and manipulate dynamic variable names.

Ronald
Yeah, I never knew about $$ trick.

Thanks Mark...
May 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ralph Seguin | last post by:
Ok... I am having problems getting variables/values INTO Flash from an external source (PHP). I'm NOT having problems getting loadVariablesNum() to _SEND_ variables via POST, but I _AM_ having...
4
by: Jonay Herrera | last post by:
hello, I'm trying to learn the basics of PHP, but when I try to run a little simple script I received a error on my screen: Notice: Undefined variable: verzenden in c:\program...
2
by: Robert | last post by:
I am using the php4-cgi Debian package which is based on PHP 4.3.4. I have problems when running php4 on the command line. If I say something like $ php4 index.cgi a number of variables...
13
by: Robert Smith | last post by:
I'm doing a website development course and during an exercise my teacher gave me to do at home I was confronted with errors. Surprisingly, those that did the exercise in class did not receive...
7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
7
by: Paul Marshall | last post by:
Hi I am my wits end! I have a PHP script running that captures variables posted from a form on the previous page. The script then emails the results using the mail() function. The script is...
4
by: Chuck Anderson | last post by:
I use apache, Php and MySql on my Windows XP machine at home. I have been doing so successfully now for a long time. However, I am now trying to use fopen to open secure URLs (https) and having...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.