Can you refer to a var using a string in PHP? [duplicate] - php

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.

Related

Dynamic variable in PHP [duplicate]

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.

What is meant by $$$var. IS it works in php [duplicate]

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.

Undefined index: string in (...) [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I need to switch on E_ALL and get the in the title mentioned warning, when I am exploding the $_GET string:
$input = explode( '/', $_GET['string'] );
Where does that come from? Is it the missing 3rd parameter (limit) for explode ? I want all entries.
Cheers
You need to make use of the isset construct first.
<?php
if (isset($_GET['string'])) {
$input = explode('/', $_GET['string']);
} else {
echo "The string was not passed!";
}

multi variables in python like $$var in PHP [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 9 years ago.
In PHP like below
<?php
$a = 'b'
$b = 'c'
echo $$a
?>
Output: c
Is there any similar kind of implementation in Python like $$..?
no .... not really you can do stuff like
a = 'b'
b = 'c'
globals().getattr(a,None)
or even better use a dict (you should really do this!!!)
env = {'a':5,'b':'a'}
env[env['b']]
but there is nothing like $$a

handling a string as PHP [duplicate]

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;

Categories