This question already has answers here:
PHP - concatenate or directly insert variables in string
(15 answers)
Closed 6 years ago.
I have a question about dynamic variables. (I have trouble searching because I have a hard time describing my problem)
In this example:
$x = 1;
$var = "A$x";
echo $var; //prints 'A1'
Now my question is, is there a way to combine "computation" without adding another variable?
What I want to do is:
$x = 1;
$var = "A($x+1)";
echo $var; //I want to output to be 'A2' but it gives 'A(1+1)'
I know that this works:
$var = "A".($x+1)
But this is not applicable to the program that I am doing. $var is initiated on the beginning of the program and will be used at the end waiting for any value of $x.
You need to concatenate your output.
$x = 1;
$var = "A". ($x + 1);
echo $var;
In your example the "+1" is inside the quotes and thus is a literal string.
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I know what is $var and $$var will do. But in an interview they give me a problem which contains $$$var. What is that means actually. I cant find any reference for that.
It is Variable variables. This will make you understand:
<?php
$a = 123;
$b = 'a';
$var = 'b';
echo $var; //b
echo $$var; //a
echo $$$var; //123
Simply you can understand the flow:
$var is a variable and it hold value.
$$var means the new variable that name is the value of $var.
and $$$var means the new variable that name is the value of $$var.
example:
$var = 'app';
so $$var will be $app.
and so on.
This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 8 years ago.
What I mean is can I do something like this...
$number = 1;
$varname = 'number';
Now I want to get the value of $number by using $varname so something like...
echo $($varname);
Output:
1
You need to use {} instead of ():
echo ${$varname};
Or even shorter:
echo $$varname;
Which equals:
echo ${'number'};
But as kingkero pointed out: You probably want to do something like ${'number'.$index} and that is easier solved with arrays.
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:
PHP variable variables
(6 answers)
Closed 8 years ago.
I'm trying to create a variable and name it after the content of another variable.
For example
$newname = "x";
//dosomething to create the variable $x
They are called variable-variables:
${$newname} = 'stuff';
If you have more, consider using an array and extract variables from it:
$vars = array(...); // keys = variable name, value = variable value
extract($vars, EXTR_SKIP);
Check this :
$a = 'hello';
$$a = 'world';
echo "$a ${$a}";
echo "$a $world";
Hope to be useful.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP: Calculate a math function f(x) in a string
I have a little problem. I have a string, such that,
$a = '(2+3+4)';
How can I handle $a such that the output, echo $a should be 9 instead of just giving (2+3+4).
If your code is indeed $a = (2+3+4), then echo will output 9.
It sounds like you have it a string. You could eval() that string to get the 9.
Have a look at php's Eval function
$string = "(2+3+4)";
eval('$a = '.$string);
echo $a;