I have a for-loop ($x++) within another for-loop ($i++) , and I want both $x AND $i to be part of a variable variable:
${'name'.$x.'place'.$i.''} = ...;
Such that I get:
$name1place1
$name1place2
$name1place3
$name2place1
$name2place2
$name3place1 etc. etc.
However, setting variables in the way quoted above does NOT work for me (i.e. with single quotations and two variable variables). I get the error "Notice: Undefined variable [...]".
The following works:
${"name$x"} = ...;
(using double quotations and just one variable variable.)
How can I set variable variables with both $x and $i within the variable name? Thank you!
You can do this by using curly braces within your variable name assignment to separate $x from the place:
$x = 4;
$i = 5;
${"name{$x}place{$i}"} = "test";
echo $name4place5;
Output:
test
However it would really make a lot more sense to just use an array:
$name[$x][$i] = "test2";
echo $name[$x][$i];
Demo on 3v4l.org
Related
I have a case where I need to check if a variable is in array and since more and more data is added I may need to assign two or more ranges of numbers to a variable for the check.
I tried appending another range to the variable and I also tried array_merge().
$x = range(0,10);
$y = range(15,30);
$z = array_merge($x, $y);
print_r($z);
Is there a more convenient way to keep the variable name $x rather than having to add each additional range with another variable ($y) then merging it with $x and assigning yet another variable ($z) to the array_merge()?
I need something like $x is in range of both numbers from 0 to 10 but also from 15 to 30 without the extra math?
$x = range(0,10);
$x .= range(15,30);
...
Thank you!
You can merge the original range() with the new range, and assign it back to $x. You do not have to give it a new name for every new value it gets, you can overwrite the value instead.
$x = range(0,10);
$x = array_merge($x, range(15,30));
print_r($x);
Is there any way to make an variable not redeclarable in PHP?
I mean if I set $a = 2; and then $a = 3 the value of variable $a should still be "2".
You can use define for declaring constant :
define('MY_VALUE','2');
echo MY_VALUE;//will always gives output 2
OR
echo constant("MY_VALUE");//give same output as above
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I got this error message
Undefined variable: x in ../../../../.php on line 35
I get the error on this line.
$x .= $y->getContent();
This line of code is in a foreach loop.
How do I get rid of the error message.
If I replace the .= with just = I'm not getting the correct output.
I hope I provided enough information
And what does .= do?
Thanks in advance.
.= is used (in your code) to concatenate the value of $x with result of ->getContent() call on $y and write the result back into $x.
Is like write $x = $x.$y
Of course if $x does not exists (like in your example I suppose; with "not exists" I mean that hasn't a value), regardless how you wrote your expression, this will fail. Moreover, $x and $y will be considerated strings so, please pay attention to your variables type (you can't concatenate two object, for example)
$x.=$y is a shortcut for $x=$x.$y
so if $x = 'cat' and $y = 'fish' then the result of $x.=$y is 'catfish'
As to your error, you need to create the variable $x 1st, out side the loop:
$x='';
foreach($var as $y){
$x.=$y;
}
$x = '';
foreach()
{
$x .= $y->getContent();
}
*it is must to define $x becouse you are append value in $x *
Define x before your loop :
$x = '';
The .= operator is a string operator to concatenate strings.
$x .= $yis the same as $x = $x.$y
You could read about string operator
s in the official PHP reference
If $x don't exist you can't concatenate it with $y, this is the reason of the error message.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a and $$a is.
$a represents a variable
$$a represents a variable with the content of $a
example:
$test = "hello world";
$a = "test";
echo $$a;
output will be hello world
If $a = 'b' then $$a is $b.
This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).
$variable is a normal variable
$$variable takes the value of a variable and treats that as the name of a variable
eg:
$var = 'welcome';
echo $var //prints welcome
$$var = 'to stackoverflow';
echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow
Double dollar is a powerful way to programmatically create variables and assign values them.
E.g:
<?php
$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen
?>
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
$a is the contents of the variable a, $$a is the contents of the variable named in $a.
Don't use this syntax in your own code.
$$a is a variable which name is in $a
Assuming $a = "foo";, $$a will be same as $foo
In PHP each variable starts with an $.
So for example you have the variable $a = 'var';
So $$a == $var
This new variable will have the "content" of the other variable as name.
$var = "array";
$$var=array("1","2");
How can I call the array in $$var without using foreach? I want a method like $$var[0], but this doesn't work.
Use it like this:
echo ${$var}[0];
When you run this piece of code:
$var = "array";
$$var = array("1","2");
...it is identical to this:
$array = array("1","2");
So you can do this:
echo $array[0];
...or this:
echo ${$var}[0];
If you must use variable variables - and 99.99% of the time arrays are a better solution - you should always use braces for clarity and disambiguation.
See the variable variables reference for more information.
You need some curly braces:
${$var[1]}
OR
${$var}[1]
depending on which variable you are accessing.