Virtual Brain Online Logo

Bookmark: Root \ PHP \ Create a dynamic variable with PHP

Create a dynamic variable with PHP


Last Updated: 2006-04-20

jimmygoon at gmail dot com
24-Mar-2006 09:03
I had an application for this a while back and couldn't find a solution and ended up spending a while testing things myself until I found a solution.

I'm adding this for Google's sake so that others can find it. I searched for: dynamic variables, using contents of variable as variable name, declaring variables as variable names, etc.

So here's what I got:

<?php

$foo = "bar";
${$foo} = "goo";
echo $bar;

?>

This example set the variable $foo to "bar"... then declares a new variable from the contents of $foo and sets its value to "goo"... effectively that line translates to $bar = "goo". then I output the value of $bar just to prove the point and its works as assumed... it prints "goo"...

Hope that helps someone else out!

Originally Posted by Skylinux @ 2006-04-19 21:54:27

 

No Comments yet .....

 

Add Your Comment:

Note: All posts require administrator approval. Please allow 24 hours for message approval.

Name:
E-Mail:
Title
Plain text only, less then 65 000 characters.

Please write the following string into the box below: QWERTY

Please answer the question above and type the answer into the text box below.