I need right syntax of variable - php

I tried to create this variable:
<?php
echo ${"product['id']"};
?>
But it is wrong. Who can help with right syntax?

<?php
echo $product['id'];
?>
You don't need to wrap the variable into string if you want to print just one variable. Php will do it for you
If you need to generate variable name in code you can try this:
$var1 = 'hello';
$var2 = ' ';
$var3 = 'world';
for ($i = 1; $i <= 3; ++$i) {
$varName = 'var'.$i;
print $$varName; // use $$ to use string as a variable name
}
Result will be hello world
This is the equivalent for previous example
$var1 = 'hello';
$var2 = ' ';
$var3 = 'world';
$vars = array(1, 2, 3);
foreach ($vars as $var) {
$varName = 'var'.$var;
print $$varName;
}

Related

PHP Loop Dynamic Variable

I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:
$ct = 1;
foreach ($record as $rec){
$var.$ct = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?
You're looking for variable variables.
Create the variable name as a string, and then assign it:
$ct = 1;
foreach( $record as $rec )
{
$name = 'var'.$ct;
$$name = $rec['Name'];
$ct++;
}
echo $var1;
It would be much better to create an array, though:
$names = [ ];
foreach( $record as $rec )
{
$names[] = $rec['Name'];
}
echo $names[0];
You can use different syntax with {}
$ct = 1;
foreach ($record as $rec){
${'var' . $ct++} = $rec['Name'];
}
echo $var1;
Although isn't it better just to use an array?
Working fiddle
You can with a double $.
$var = "variable";
$$var = "test";
echo $variable;
//echoes "test"
in your example:
$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
$$varname = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
Please try this, let me know if it works for you.
I use a prefix with the dynamic variable.
$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
$temp = $ct;
$ct = $prefix.$ct;
$$ct = $rec;
$ct = $temp + 1;
}
echo $var1;
You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.
In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.
$var = [];
foreach( $record as $k => $rec )
{
$var['var'.$k] = $rec['Name'];
}
extract($var);
echo $var0; //or $var_1 or $var_2

php could not use $$ with array

I know that
$b = 1;
$var = "b";
$$var = 2;
echo $b;
will show 2
But when I try it on array, it fail
$c[1] = 1;
$var = "c[1]";
$$var = 2;
echo $c[1];
$d[1] = 1;
$var = "d";
$$var[1] = 2;
echo $d[1];
they both show 1, why?
In your first example, you can't use the index because it is assumed part of the variable name.
In the second, you need to use the curly braces for a complex syntax to disambiguate. This way PHP knows that it's the contents of $d[1] and not the contents of $var[1].
$d[1] = 1;
$var = "d";
${$var}[1] = 2;
echo $d[1];

php - use variable variable in while loop

I'm a bit confused about variable variables.
What I like to do is print the value of $var1, $var2 and $var3:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo $var.$i;
$i++;
}
I know $var.$i; is not correct, but I think it shows what I would like to achieve; the while-loop should change it to $var1, $var2 and $var3;
I've tried the following:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
$current_var = 'var'.$i;
$current_var = $$current_var;
echo $current_var;
$i++;
}
But that doesn't work. I think because $var1, $var2 and $var3 are recreated in the while-loop instead of using the actual value. Not sure if that's correct, but that the only thing I can think of.
Try this instead:
echo ${"var".$i};
Curly braces can resolve to variable names without having to use the dollar-dollar approach.
See: Variable Variables in PHP
Try this one.
<?php
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo ${'var'.$i};
$i++;
}
?>
Are you trying to do something like this. Then use array
$my_data = array();
$my_data[1] = 'a';
$my_data[2] = 'b';
$my_data[3] = 'c';
// Method 1
$i = 1;
while ($i <= 3) {
echo $my_data[$i];
$i++;
}
// Method 2
foreach ($my_data as $data) {
echo $data;
}
// Output
abc
abc

php parsing comma separated list of var=value pairs

I'm receiving a string
a=100,b=20,c=20;
for instance I know exploding this into an array using , as delimiter,
and it becomes
a=100
b=20
c=20
as strings,
how can this be further evaluated as variables inside php so I could do
echo $a //100
echo $b //20
echo $c //20
try the function "parse_str",like
$str = str_replace(',','&',"a=100,b=10,c=20");
parse_str($str);
echo $a;
echo $b;
echo $c;
$string = 'a=1,b=20,c=43';
foreach ( explode(',', $string) as $expression ) {
list($variable,$value) = explode('=', $expression);
$$variable = $value;
}
echo 'a = ', $a, ' and b = ', $b, ' and c = ', $c, PHP_EOL;
Cheers
Depends how "dangerous" you want everything to be - you can always use eval but then there are many issues to take into consideration.
Example:
eval("\$str = \"test\";");
echo $str;
Try This
$input = 'a=100,b=20,c=20';
$items = explode(',',$input);
foreach($items as $i){
$pair = explode('=',$i);
$$pair[0] = $pair[1];
}
$input = 'a=100,b=20,c=20';
$statements = explode (',', $input);
foreach ($statements as $s)
eval ('$' . $s); // THIS IS EVIL!!!! TOO MANY SECURITY PROBLEMS
echo $a; // will output 100
echo $b; // will output 20
echo $c; // will output 20
You can use variable names for variables. Take for example:
$var = 'var2';
$$var = 5;
echo $var2;
this echoes 5 to the command line.
$var evaluates to being var2, and $var2 is set to 5.
Taking your code we can create this:
$string = 'a=100,b=20,c=20';
$array = explode(',',$string);
foreach($array as $element) {
$temp = explode('=',$element);
$$temp[0] = $temp1;
}
echo $a.' '.$b.' '.$c;
This echoes the string '100 20 20'.

Merging Variable PHP

im confuse that...
like example:
$Q1 = "hello";
$Q2 = "world";
$Q3 = "StackOverflow";
$i = 1;
while($i < 3) {
$a = "$Q".$i; //I think this is wrong.
echo $a; // i tried ${$a} doesn't work =/
$i++;
}
then output format:
$Q1
$Q2
$Q3
but there is not output like this:
hello
world
StackOverflow
I want like $Q + $i become $Q1 to answer is: "hello"...
$varName = 'Q'.$i;
$a .= $$varName;
Or just
echo $$varName . "<br>\n";
To create the variable variable, use:
$a = ${'Q'.$i};
echo $Q1 . $Q2 . $Q3; will output what you're looking for.
Alternatively, you could do this:
$a = '';
for($i = 1; $i <= 3; $i++)
$a .= ${'Q' . $i};
echo $a;
What you are doing there is simply printing the string '$Q1', '$Q2' and '$Q3'. In PHP you use dynamic variable names this way:
<?php
$Q1 = 'hello';
$Q2 = 'world';
$Q3 = 'StackOverflow';
for ($i = 1; $i <= 3; $i++) {
echo ${'Q' . $i};
}
?>
PHP does support variable variable names, denoted using $$. This will do what you want.
$qvar = 'Q'.$i;
$a = $$qvar;
However, this is considered very poor practice -- almost as bad as using eval() (and for similar reasons).
The correct answer would be to create an array of $Q, and referencing array elements;
$Q = array(
"hello",
"world",
"StackOverflow")
$a = $Q[0] . $Q[1] . $Q[2];
Yeah. When you have double quoted strings, and you put a dollar sign and something else in it, it interprets it as a variable. (it also escape things like \n)
Example
$test = "hi";
echo "$test world"; //This outputs hi world
In your case, $Q doesn't exist. The default PHP behaviour is to ignore that error and just puts out nothing. This is why it's recommended to report all problems with your code. Check out
http://php.net/manual/en/function.error-reporting.php for details.
Solution to your problem would be using single quoted strings. do $a = '$Q'.$i;
$Q = array("hello", "world", "StackOverflow");
foreach($Q as $w) {
echo $w;
}
If you can't do something like this then you will need to use dynamic variables:
$var = 'Q' . $i;
echo $var;

Categories