Hi, 🙂

I have read the lesson "Variable Variables, PHP, and You" end i have a big question:
With the exemple, if
$x = "this";
$$x = "is cake";
we can see the value of $this
but how can we do, if the value of $x came from a datebase, to see the value of this new variable? :-?

Thanks for the answer and sorry for my english (i'm french)...

tewebe

    Originally posted by Barthak
    How about ${$x}???

    when i do this:

    while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
    {
    $x1 = "droits_" . $row["objet"] . "[1]";
    .../...
    }

    with $row["objet"] value is "membre" for exemple, i want to use the value of the variable name $creation_membre[1], but using ${$x1} in an echo to see his value, show me this:

    $x1=droits_membre[1] et ${$x1}=

    $($x1) seem to be N/A...
    i'll have alsa to note that the "creation_membre" array is the name of input field from a form:

    <input type=checkbox name=droits_membre[] checked>
    <input type=checkbox name=droits_membre[] checked>
    <input type=checkbox name=droits_membre[] checked
    <input type=checkbox name=droits_membre[] checked>

      Originally posted by tewebe


      when i do this:

      while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
      {
      $x1 = "droits_" . $row["objet"] . "[1]";
      .../...
      }

      with $row["objet"] value is "membre" for exemple, i want to use the value of the variable name $creation_membre[1], but using ${$x1} in an echo to see his value, show me this:

      $x1=droits_membre[1] et ${$x1}=

      $($x1) seem to be N/A...
      i'll have alsa to note that the "droits_membre" array is the name of input field from a form:

      <input type=checkbox name=droits_membre[] checked>
      <input type=checkbox name=droits_membre[] checked>
      <input type=checkbox name=droits_membre[] checked
      <input type=checkbox name=droits_membre[] checked>

        $x1 = "droits_" . $row["objet"] . "[1]";

        this will merely make $x1 a string. Not a variable.
        print( $x1 ); will parse: droits_membre[1].

        you might wanna do it like this:

        $x1 = ${droits_ . $row['objet']}[1];

        Now you'll have a variable, variable array.?

          Originally posted by Barthak
          $x1 = "droits_" . $row["objet"] . "[1]";


          this will merely make $x1 a string. Not a variable.
          print( $x1 ); will parse: droits_membre[1].

          you might wanna do it like this:

          $x1 = ${droits_ . $row['objet']}[1];

          Now you'll have a variable, variable array.?

          Thanks, I will try and say you if it is good or not... 🙂
          It's difficult to me to explain in english but I'm happy to have find this forum 🙂

            5 days later

            It work Barthak, thanks so lot 🙂

              Take a look at the code below. I run it on a PHP3 machine.

              The table is a quick way to show what happens to the variables with the box checked & un-checked.

              It works perfectly.

              <?PHP
              //$checked = "Yes";
              $var1="Loser";
              if ($$var1 == "Yes") {
              $$var1 = "checked";
              }
              else {
              $$var1 = "";
              }

              echo "
              <html>
              <head>
              <title>Check</title>
              <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
              </head>

              <body bgcolor=\"#FFFFFF\" text=\"#000000\">
              <form method=\"post\" action=\"check.php\">
              <table width=\"228\" border=\"1\" cellspacing=\"2\" cellpadding=\"2\">
              <tr>
              <td width=\"74\">Value-->></td>
              <td width=\"40\">

                  <div align=\"center\"> 
                    <input type=\"checkbox\" name=\"$var1\" value = \"Yes\" ${$var1}>", $var1;
              	  echo"
                  </div>
              
              </td>
              	<td width=\"86\">$checked</td>
                  <td width=\"86\">$$var1</td>
              	<td width=\"86\">${$var1}</td>

              </tr>
              <tr>
              <td colspan = \"3\" align = \"middle\">
              <input type=\"submit\" value=\"Submit\"></td>
              </tr>
              </table>
              </form>
              </body>
              </html> ";
              ?>

                A follow-up to this post,

                I noticed that I have stored several options in a table;

                "blue", "light blue", "green",...

                Where the use of the "variable variable" falls apart, is if I try to detect $$var1, where $$var1 contains "light blue"

                if ($$var1 == "Yes") {
                $$var1 = "checked";
                }

                If I echo $$var1, it shows up empty..

                The SPACE between the characters is the problem......

                Have not figured out a fix yet.

                Does anyone have an idea?😕

                  <?php
                  ${'light blue'}='Yes';
                  $var1='light blue';
                  if ($$var1 == "Yes") {
                  $$var1 = "checked";
                  }
                  echo $var1.'<br>'.$$var1.'<br>'.${'light blue'};
                  ?>

                  Or just use arrays.

                  PS: get_defined_vars() can also be pretty useful.

                    echo $var1.'<br>'.$$var1.'<br>'.${'light blue'};

                    This is an unknown variable "light blue". It is pulled out of a table at form display time, hence you cannot use your syntax.

                    it has to be..

                    echo $var1.'<br>'.$$var1.'<br>'.${'$var1'};

                    & as I said, the space in the phrase
                    "light blue" is causing the variable to be displayed as a blank....

                      Write a Reply...