Creating Variable Name by Concatenating Strings in PHP

creating variable name by concatenating strings in php

Variable variable is not recommended, but the answer is below:

$question = ${'question'.$var}[0];

How to concatenate PHP variable name?

The proper syntax for variable variables is:

${"check" . $counter} = "some value";

However, I highly discourage this. What you're trying to accomplish can most likely be solved more elegantly by using arrays. Example usage:

// Setting values
$check = array();
for ($counter = 0; $counter <= 67; $counter++){
echo $counter;
$check[] = "some value";
}

// Iterating through the values
foreach($check as $value) {
echo $value;
}

PHP- concatenate string with iterator to get original variable name

Use it this way.

Try this code snippet here

<?php
$encoded1=10;
$encoded2=20;
$encoded3=30;

for($i =1;$i<=3;$i++)
{
echo ${"encoded".$i};
}

PHP - concatenate or directly insert variables in string

Between those two syntaxes, you should really choose the one you prefer :-)

Personally, I would go with your second solution in such a case (Variable interpolation), which I find easier to both write and read.

The result will be the same; and even if there are performance implications, those won't matter 1.


As a sidenote, so my answer is a bit more complete: the day you'll want to do something like this:

echo "Welcome $names!";

PHP will interpret your code as if you were trying to use the $names variable -- which doesn't exist.
- note that it will only work if you use "" not '' for your string.

That day, you'll need to use {}:

echo "Welcome {$name}s!"

No need to fallback to concatenations.


Also note that your first syntax:

echo "Welcome ".$name."!";

Could probably be optimized, avoiding concatenations, using:

echo "Welcome ", $name, "!";

(But, as I said earlier, this doesn't matter much...)


1 - Unless you are doing hundreds of thousands of concatenations vs interpolations -- and it's probably not quite the case.

PHP concatenate variable

try ${$test.$i} = value

EDIT: http://php.net/manual/en/language.variables.variable.php

how to create and concatenate dynamic variables in PHP?

You can use curly brackets for that:

${'menu' . $i}

but I strongly advise you just to echo the stuff you need inside the loop.

Plus, mysql_* functions are deprecated - go either the PDO or the mysqli way please.

How to combine two PHP variable names to call one?

The Problem

Your current code echo "$Variable" . $Iteration1; tries to echo the value of the variable $Variable, which doesn't exist, and concatenate $Iteration1.

The Solution

What you want to do is build a string, "Variable" . $Iteration1 (e.g., $Variable2), then get the value of the variable with that name. This is called a "variable variable." You do this by writing ${string_you_want_to_create}, as in ${"Variable" . $Iteration1}.

Example Code For your Problem:

$Variable1 = '5/5/15';
$Variable2 = '6/13/76';
$Variable3 = '5/8/15';

$Iteration1 = 1;

while ($Iteration1 <= 3) {

echo ${"Variable" . $Iteration1} . "\n";

$Iteration1++;
}

Output:

5/5/15
6/13/76
5/8/15

Note: You could also do this in two steps, like this:

$variableName = "Variable" . $Iteration1;
echo $$variableName; // note the double $$


Related Topics



Leave a reply



Submit