I want to append some $variables automaticly and set their names numeric
I have a script look like this:
<?php
$i=0;
while($i<=100){
$variable_[$i]=$i;
$i++;
#with "[$i]" I mean their name will be $variable_1 , $variable_2, $variable_3 ...
#they will be automatic increased variables non manual!
}
?>
This is called variable variables.
You can set a variable variable by defining its name inside a variable, such as:
$name = 'variable_' . $i;
and then assign a value to it by doing:
$$name = $i;
Note that variable variables can easily be misused. Make sure you completely understand the repercussions of this feature on your code and the risk of having bugs, and ensure this is the only solution you have, i.e. you can't use an array ($variables[$i] = $i;) instead.
Its better to use Array with key=> value pair. You can build this array dynamically and then loop through it by using foreach.
Related
Say for example that I have this variable
$p0001 = array("title"=>"This is the title","name"=>"Just Me");
And the URL is
https://www.example.com?id=p0001
How do I get the correct array from PHP? I've tried
echo $_GET["id"]["title"];
To explain little more, say I have two array variables. I want it to echo the "title" from from the array $p0001. So how do I make sure I gets the variable I put in $_GET?
You can use Variable variables
So, your variable name is $p0001. Your GET parameter id is basically a pointer to the variable name, so using variable variables, we can reference the variable we're looking for:
$varname = $_GET['id']; // $varname = 'p0001';
$$varname; // this is basically $p0001
$$varname['title']; // and you can get your title from $p0001
You could also check if a variable exists before using with isset($$varname)
I was wondering if it's possible to put a PHP variable inside $_POST[''] like this:
$_POST[$variable];
I'm asking this because I have a page in which all the inputs have dynamic names according to how many orders the user has made and when I must retrieve their values through post, I never know the names but if I would have something like $_POST[$variable] I would know. The variable is a string and so it would turn out like $_POST['String'].
So is it possible to do something like this?
$numero = $count_ficha;
$countU = 1;
for ($i = 1;$i < $numero + 1;$i++) {
$identificador = "identificadorNI".$countU;
$identificador2 = "identificador".$countU;
$id_subencomenda = $_POST[$identificador];
$countU++;
echo $id_subencomenda;
}
Yes, it is possible to use a variable inside $_POST[''] such as $_POST[$variable]
You will still be required, however, to define a value for $variable
It is posible, but if you don't know the key of $POST[] that you're trying to get the value of, how could you know what value that you have to put on $variable?
For unknow values, the best you can do is a foreach, so you can iterate all the $POST array and then you can do whatever you want with the values, like this
foreach ($_POST as $key) {
//do whatever you want with the $key
}
Check PHP foreach
if you have a dynamic value for the page you just use some $variable because $_POST is use to send information from some HTTP POST method.
(http://php.net/manual/en/reserved.variables.post.php)
maybe you can resolve your problem by creating a global variable.
I have a array containing to which I have created local variables from using the extract() function with the EXTR_PREFIX_ALL flag set. Afterwards, I called compact() on the new prefixed variables created by extract() but displaying the array created from compact() using print_r() gives an empty array(). Sample code follows:
<?php
$cities = array('City1' => "Chicago", 'City2' => "Boston");
extract($cities, EXTR_PREFIX_ALL, "new");
echo "City 1: {$new_City1} City 2: {$new_City2}" . "<br><br>";
$new_cities = compact($new_City1, $new_City2);
print_r($new_cities);
?>
I am using PHP version 5.6. What am I doing wrong here?
In your current code, you're actually providing the values of each variables you're trying to compact, not the name of the variables. It acts like a variable variables behavior. In order to make it work properly, you provide the variable names as strings or in array form:
$new_cities = compact(array('new_City1', 'new_City2'));
// or
$new_cities = compact('new_City1', 'new_City2');
Here's the excerpt from the manual:
Parameters
varname1
compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.
Give it a try
$new_cities = compact('new_City1', 'new_City2');
print_r($new_cities);
For more info
I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.) for this, not the plus sign (+).
If the value of $row_ColNumc['ColNaumNo'] is 32 then the value stored in variable $name is 'T32R';
You can then prepend the variable $name with an extra $ to use it as the name of another variable (indirection). The code echo($$name); prints the content of variable $T32R (if any).
If the variable $T32R stores an array then the syntax $$name[0] is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
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 can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);
Let's say I have some variables declared - but I don't know exactly which, I just have an array with variable names.
$variable_list = array('var1', 'var2', 'var3', 'var4');
We go ahead and assign some values.
foreach($variable_list as $var_name){
$$var_name = rand(100,1000);
}
Now I want to unset these variables in a similar fashion. Not remove them from list, but unset the ACTUAL variable.
foreach($variable_list as $var_name){
unset($var_name);
}
this does not work. any ideas?
foreach($variable_list as $var_name){
unset($$var_name);
}
PHP Manual
Why do you set them using variable variables ($$var_name) and don't unset them like that? This should work:
foreach($variable_list as $var_name){
unset($$var_name);
}
However, since you say:
Now I want to [...] not remove them from list, but unset the ACTUAL variable.
Simply use:
foreach($variable_list as $var_name){
$$var_name = null;
}