0

I am a PHP beginner. I want to create multiple variables and the name of those variables should have a value appended to it which depends on a counter variable of a for loop.

  foreach ( $xmlTagNames as $xmlKey => $xmlTagName )
  {
    if ($xmlTagName == "property")
    {
        for($i = 0; $i < 4; $i ++)
        {
            $$xmlTagName = $xmlFile->createElement ( $xmlTagName );
        }
    }
   }

At the seventh line I want the variable names like $property0,$property1 etc. How could this be done?

2
  • This is what arrays are for. Commented Jul 11, 2014 at 9:48
  • 1
    Pretty much whenever you think you need variable variables (look for that term in the manual), you actually want arrays (look that up too). Yes, really. Commented Jul 11, 2014 at 9:50

3 Answers 3

3

As @David said, arrays are probably what you should use:

$properties = array();
foreach ( $xmlTagNames as $xmlKey => $xmlTagName )
{
  if ($xmlTagName == "property")
  {
      for($i = 0; $i < 4; $i ++)
      {
          $properies[] = $xmlFile->createElement ( $xmlTagName );
      }
  }
}

Then you can access the properties using $properies[0], $properies[1], $properies[2] and $properies[3].

But if you insist on creating variables with incrementing names you could use the following:

foreach ( $xmlTagNames as $xmlKey => $xmlTagName )
{
  if ($xmlTagName == "property")
  {
      for($i = 0; $i < 4; $i ++)
      {
          $variableName = $xmlTagName.$i;
          $$variableName = $xmlFile->createElement ( $xmlTagName );
      }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I agree with the comments: You should use an array here.

To answer your question though, use the following syntax:

${$xmlTagName . $i} = $xmlFile->createElement ( $xmlTagName );

1 Comment

Can you change the name of a variable during run time? Or am I understanding the question wrong?
0

I agree with the other comments that you would be better using an array here

$properties = array();

foreach ( $xmlTagNames as $xmlKey => $xmlTagName )
{
    if ($xmlTagName == "property")
    {
        for($i = 0; $i < 4; $i ++)
        {
            $properties['property' . $i] = $xmlFile->createElement ( $xmlTagName );
        }
    }
}

var_dump($properties);

If you are set on using variable variables

foreach ( $xmlTagNames as $xmlKey => $xmlTagName )
{
    if ($xmlTagName == "property")
    {
        for($i = 0; $i < 4; $i ++)
        {
            $variableName = $xmlTagName . $i;
            $$variableName = $xmlFile->createElement ( $xmlTagName );
        }
    }
}

1 Comment

No need for double array, as the value of $xmlTagName is always "property" in $properties[$xmlTagName][$i].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.