php could not use $$ with array - php

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];

Related

How do i turn an string into variables and still get a good output

How do i turn an string into variables and still get a good output.
$text = "showandreturn";
$disp = str_split($text, 2);
$firstnum = 3;
for($b = 0; $b<$firstnum; $b++){
$a = "$disp[$b]"; #showan
}
$b = "ENDED";
for($b = $firstnum; $b<sizeof($disp); $b++){
$c = "$disp[$b]"; #dreturn
echo = "$a$b$c";
}
my current output with this code is . andranetanurann ..
And I want beter result like showanENDEDdreturn
Thanks for your time and understanding..
Are you trying to put ENDED in the middle of the string?
In that case have a look at this:
$text = "showandreturn";
$b = "ENDED";
$len = strlen($text); // lenght of string
// Output half string + $b + rest of string
Echo substr($text, 0, floor($len/2)) . $b . substr($text, floor($len/2));
https://3v4l.org/Ea6tn
You are overwriting your variables every time it loops so only the result of the last loop is stored in the variable.
This code should return the required output.
$text = "showandreturn";
$disp = str_split($text, 2);
$num = 3;
$a = '';
$c = '';
for($b = 0; $b<$num; $b++){
$a .= $disp[$b];
}
$b = "ENDED";
for($bn = $num; $bn<sizeof($disp); $bn++){
$c .= $disp[$bn];
}
echo $a.$b.$c;
Alternative 1
What you need is variables variable
$foo = 'bar';
$foo = 'magic';
echo $foo; //Outputs magic
Alternative 2:
you can check http://php.net/manual/en/function.parse-str.php
parse_str($"My Value=Something");
echo $My_Value; // Something
Alternative 3:
echo eval('return $'. $string . ';');

I need right syntax of variable

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;
}

PHP: Passing parameter by reference didn't work

I'm creating a function which passing parameter by reference. I just can't get return value with this code and I need your help.
function wow ($id, &$a, &$b)
{
$detail[0][0] = 1;
$detail[0][1] = 2;
$detail[0][2] = 3;
$detail[0][3] = 4;
$detail[1][0] = -1;
$detail[1][1] = -2;
$detail[1][2] = -3;
$detail[1][3] = -4;
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
else
{
$b = $detail[$i][0];
$b = $detail[$i][1];
$b = $detail[$i][2];
$b = $detail[$i][3];
}
}
This is the way I call the function.
$a = $b = null;
wow(1, $a, $b);
echo $a[0]." ".$a[1]." ".$a[2]." ".$a[3]." ".$a[4]." ".$b[0]." ".$b[1]." ".$b[2]." ".$b[3]." ".$b[4]." ";
I think you are missing how arrays work:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
Each of these statemenets is overwriting the previous one.
Try something like this:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i];
}
That was you are copying the entire array inside the first one into $a.
Additionally, you set both $a and $b to null, yet only set one or the other in your function - you will always return at least a warning telling you about a null variable you are trying to echo.

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

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