Forum Moderators: coopster
I have simply got hundreds of varibles that I need to access from the $_POST array. Is there an efficient way of doing the below:
$langactx = $_POST['langactx'];
$langc = $_POST['langc'];
$langcplusplus = $_POST['langcplusplus'];
$langcob = $_POST['langcob'];
$langdcom = $_POST['langdcom'];
$langdelphi = $_POST['langdelphi'];
$langfort = $_POST['langfort'];
$langjava = $_POST['langjava'];
$langjavabeans = $_POST['langjavabeans'];
$langoracledev = $_POST['langoracledev'];
You get the idea. I don't fancy writing hundreds more statements in this way, there must be a better way.
Thanks for any help
Each variable will contain " ", "B", "I", and "A".
And I want to pass the variables through the varbia function below
function varbia($test) {
if ($test == "B") {
echo "Beginner";
} elseif ($test == "I") {
echo "Intermediate";
} elseif ($test == "I") {
echo "Advanced";
}
I was thinking that if I had all the variables accessable without the $_POST array calling on each variable I'd find it easier to program the html output I want for the email.
One thing of value I might add though, is this:
switch($test) {
case "B":
print('Beginner');
break;
case "A":
print('Advanced');
break;
default:
print('Fill in the form dummy!');
break;
}
More here:
[dk2.php.net...]
So, anyone know how to do the variable variable thing or have another way to do it?
Nick
while(list($index, $value)=each($_POST))
{ $$index = $value;
}
i'm unable to make this work though, but I'm sure I'm on to something here.
...and thanks nick for the advice on my function
...and coopster the extract() function will help me out if I need to deal with the array in such a way
<?php
foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key];
print "$key is ${$key}<br />";
}
?>
print "$key is " . $$key . "<br />";
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
You might also have asked, well why can't I just use ${$_POST}? Because the manual specifically warns against this:
Please note that variable variables cannot be used with PHP's Superglobal arrays. This means you cannot do things like ${$_GET}.
Variable variables let you assign values to invalid variable names (those with hyphens, dots, etc.).$foo = "invalid-variable";
$$foo = "value of invalid variable";How do you access them once you've done this?
echo ${"invalid-variable"}; // value of invalid variable
Pretty cool.
Tom