Variables inside Variables.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Variables inside Variables.

Post by ThatPerson »

I was wondering how I would be able to set and get a variable with a variable name. I was wondering if there was a way to do something like:

Code: Select all

<?php
$a = "Hello";
${$a."Hi"} = "Goodbye";
echo ${$a."Hi"}; 
?>
So you could retrieve variables from inside a for loop?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Variables inside Variables.

Post by oscardog »

This should answer your questions.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Variables inside Variables.

Post by ThatPerson »

Ah, thanks. I had a look and now know what I was looking for.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Variables inside Variables.

Post by ThatPerson »

Instead of creating a new thread, I was wondering if anyone here would know why this:

Code: Select all

   $num = ${p.$a."pos"};
   $len = ${p.$a."len"};
   $named = ${p.$a};

   $test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
   $test3 = mysql_fetch_object($test);
   $test4 = $test3 -> friendrequests;
     
   $namen = $named.",";

      $test2 = str_replace("",$namen,$test4);
   echo $test2;
      mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
         $tes = mysql_query("select friendrequests from notes_notes where email = '${$named}'") or die(mysql_error());
   $tes3 = mysql_fetch_object($tes);
   $tes4 = $tes3 -> friendrequests;


      $tes2 = str_replace($namen,"",$tes4);

         mysql_query("UPDATE notes_notes SET friendrequests = '$tes2' WHERE email = '${$named}'") or die(mysql_error());
   
   
Would return the same text as before instead of $tes4 without $namen included? ($tes4 because I had used test, and $namen because I was running out of ideas for name).
Post Reply