Can I do something like this?
$captcha_results = $_REQUEST[$_REQUEST['field_name']];
I am interested in it, because I try to find out where I've made a mistake.
Thank you in advance.
Yes, of course. The one you've just asked is one of variable variables. You can learn more the PHP variables here:
PHP Variable Basics
Variable in Variable
For example, you have $var containing value of foobar. You can use the value for other references (in my case, as an index of an array), as shown below.
$var = 'foobar';
$array['foobar'] = 'bar';
echo $array[$var];
This will output bar as a result.
Related
I'm trying to modify the name of the key dynamically based on which rows are being fetched, but my syntax seems to be slightly off within the query. After moving the quotes around more times than I care to admit, I finally decided it was time to ask for help ;-)
$var = '$foo_row';
$MAX_5A = ${$var . '["MAX_5A"]'};
Instead of
$MAX_5A = $foo_row['MAX_5A'];
Bonus points if someone wants to explain to me the logic behind the correct syntax :-)
This should work for you:
(Just use variable variables with curly quotes to make sure PHP doesn't think this: ${$var["MAX_5A"]}. Also note I removed the dollar sign in the string)
$var = 'foo_row';
//^ dollar sign removed
$MAX_5A = ${$var}["MAX_5A"];
$var = 'foo';
$bar = 'var';
echo $$bar; // foo
Logic: A variable variable takes the value of a variable and treats that as the name of a variable.
in bash there is something like
$_
which is a temporary variable that stores the last argument of previous command
is there a similar variable or construct in PHP or how can I access the last used variable in the script?
this would be very handy for simple debugging functions or if you need a function that you have to add in several places at your code.
Or you could add something like:
do_some_more($_);
in several places in your code
There is no such feature in PHP.
$_ is just like any other variable.
$_ = 'hello world';
echo $_; // hello world
I'm new to PHP so please forgive me if this is a stupid question.
How do you include a variable in a variable?
What I mean is:
<?php
$variable_a = 'Adam';
$variable_b = '$variable_a';
?>
In other words the second variable is the same as the first one.
I won't bother explaining why I need to do it (it will confuse you!), but I just want to know firstly if it's possible, and secondly how to do it, because I know that code there doesn't work.
Cheers,
Adam.
Don't use the quotes, they indicate a string. Just point to the variable directly, like this:
$variable_b = $variable_a;
If you want the variables to be equal, use:
$variable_b = $variable_a;
If you want the second variable to contain the first, use variable parsing:
$variable_b = "my other variable is: $variable_a";
Or concatenation:
$variable_b = 'my other variable is: ' . $variable_a;
PHP have this advantage in producing one string variable's value based on another. To do this, write code like this:
$b = "My name is $name.";
The following code does NOT work:
$b = '$name';
Other occasions in which coding like this works are:
$b = <<<STRING
Hello, my name is $name...
STRING;
If you want to access an array, use:
$b = "My ID is {$id['John Smith']}.";
and of course,
$b = <<<STRING
Hello, my name is {$username}, my ID is {$id['John Smith']}.
STRING;
I recommend using {} because I frequently use Chinese charset in which occasion coding like
$b = "我是$age了。";
will cause PHP look up for variable $age了。 and cause error.
Either without quotes to reference the variable directly, since quotations means it's a string
$variable_b = $variable_a;
Or you can ommit the variable in double quotations, if you want it to appear in a string.
$variable_b = "My name is $variable_a";
I know it sounds so dumb but people were asking me about it and I don't have a proper answer.
Like
$var = "var1";
$var1 = "hello";
echo $$var;
Thanks!
You are talking about Variable variables :)
They're called variable variables.
It can be called "wrong program design" and "time to learn arrays"
The mechanism to specify a variable by using the value of another variable is called variable variables.
This might seem cool is some way. But I rather think that it’s an obscure technique and can also tempt to use some dangerous pattern like exporting input values to the global variable space (just like register globals does).
There are also some restrictions with variable variables:
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
This basically describes that the following is not possible:
$name = '_GET'; // identifier of the superglobal $_GET
$args = $$name;
But there’s also a workaround for this by using the $GLOBALS array:
$name = '_GET';
$args = $GLOBALS[$name];
The title is in the question (EDIT: :P I mean the question is in the title), basically can I call variable $x before defining it further down the page?
Short answer, no.
Long answer, noooooooooooooooooooooooooooooooooooooooooo.
But seriously, you can refer to it, it just won't do what you want.
Depending on how strict your warnings on you can call an undeclared variable as much as you want. However until you assign it a value it won't have a value.
I am not quite sure to understand your point but if you want to write
echo $x;
$x = "2";
you will not get "2" as a result.
PHP will usually not issue a warning when you reference a variable that has not yet been assigned a value. PHP will create it on the fly and assign it the null value which will then be casted to whatever scope you have. For example
$a = $b + 5;
echo $a;
will print 5 because in this case $b will be interpreted as beeing 0.
I hope this will help
Jerome
No, the execution goes down the file. You can use a function though, to call later on once the variable has been defined. For example:
<?php
function meow() {
echo $kitty_noise;
}
?>
And then later on down the file...
<?php
$kitty_noice = 'meowwwwww!';
meow();
?>
Horrible example....