Add an auto-incrementing suffix to a PHP variable - php

I've looked all over and I'm starting to wonder if what I'm wanting to do is possible. I've seen the loop to allow you to auto increment a number up to a cap:
for ($n = 0; $n <= 7; $n++)
However, what I'm trying to do is to add a auto-incrementing suffix to a variable that is already part of a loop coming from an html form. But I can't figure out how to get the autoincrement to work since I can't put a for() inside a foreach(). Let me see if I can give you an idea of what I'm looking for.
foreach($_POST[‘input’] as $input) {
${‘input.‘n++’}=$input
}
With the goal being that there will now be variable $input1, $input2, etc, each declared for an input received. Is this achievable?

$n=0;
foreach($_POST[‘input’] as $input) {
$varname = "input$n";
$$varname = $input;
$n++;
}
echo $input1;

Related

How do you print a variable in PHP that's either one element of an array, or the whole variable if it's not an array

Say I have $exampleVariable, which I want to print. $exampleVariable may be an array, in which case I have this set up to get the right array element, which I can then print with print $exampleVariable[$i].
if ($_GET) {
$i = array_search($_GET["exampleQueryString"], $exampleVariable);
} elseif (is_array($exampleVariable)) {
$i = 0;
} else {
$i = "";
}
My problem is that last else, if $exampleVariable is NOT an array, because then I get print $exampleVariable[] which doesn't work. So is there something I can put as $i to print the whole variable?
Alternatively, I considered including the brackets in $i, so I'd have for example $i = [0];, but in that case I don't know how I'd print it. $exampleVariable$i certainly won't work.
I have a good number of variables besides $exampleVariable I'll need to print, all with the same $i or lack thereof, so I'd like to not have to do anything longwinded to set each of them up individually.
This sounds way more complicated than I feel like it should, so hopefully it makes sense!
You can always do a nifty thing that is called type casting. That means, that you can always make a variable an array even if it is not, by prepending its name by (array):
$exampleVariable = (array)$exampleVariable;
So you don't need three if branches at all:
if ($_GET) { 
$i = array_search($_GET["exampleQueryString"], $exampleVariable);
} else {
$i = 0;
$exampleVariable = (array)$exampleVariable;
}
You could apply the (array) cast, which will have no effect if the target is already an array:
$i = array_search($_GET["exampleQueryString"], (array)$exampleVariable);

PHP- concatenate string with iterator to get original variable name

I have 20 variables with name $encode1,$encode2....,$encode20.
Now, I want to print these variable in the for loop by combining $encode.$1 to achive variable $encode1.
Loop example:
for($i =1;$i<=20;$i++)
{
$echo = $encodedImage.$i; => What to do here?
}
How could I access the names by using iterator?
Plus, I don't want to create an array. I just want to access them directly dynamically.
I haven't found any answer on stackoverflow regarding this topic. If there is any, please share me the link. Thanks!
using variables variable to achieve such a approach
Sometimes it is convenient to be able to have variable variable names.
That is, a variable name which can be set and used dynamically. A
normal variable is set with a statement such as:
for($i = 1; $i <= 20; $i++) {
$myVariable = "encoded" . $i;
echo $$myVariable;
}
Use it this way.
Try this code snippet here
<?php
$encoded1=10;
$encoded2=20;
$encoded3=30;
for($i =1;$i<=3;$i++)
{
echo ${"encoded".$i};
}

adding variable numbers in PHP loop counter

I've got a PHP loop and a counter called $total which is set to 0 before the loop. Then once the loop starts I am adding the value of $weight to the loop counter. I originally did it like this:
$total=0;
foreach ($weights as $weight){
$total = $total+$weight;
}
But realised that it also works like this:
$total=0;
foreach ($weights as $weight){
$total += $weight;
}
Question is which is the correct method or if both are correct which is the better method?
Thanks
Both are correct, and neither is better, strictly speaking. They function identically.
That said, some coding styles prefer += because it's easier to read, while others prefer $x = $x + $y because it requires more deliberate action to write, makes more obvious what is happening, and reduces the likelihood of a single-character typo.

handle null values to prevent errors while in loop

I am using the following code to return data from a loop and although I am receiving the data it seems there may be null data also which is producing errors.
Here is the code
$extensions = count($cert['tbsCertificate']['extensions']);
for ($j = 0; $j < $extensions; $j++) {
$count = count($cert['tbsCertificate']['extensions'][$j]['extnValue']);
for ($i = 0; $i < $count; $i++) {
if (array_key_exists('dNSName', $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])) {
$value = $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i]['dNSName'];
$item = $i;
echo 'DNSName',$item,' : ', $value,"\n";
}
}
}
Here is the result of output
Can someone help me update the code to check for null values (i think in the first for loop) and ideally ignore those so the warnings don't appear? I am happy to provide more context if required.
Change your if condition so that it checks if it is set and if it is array first:
if(isset($cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])
&&
is_array($cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])
&&
array_key_exists('dNSName', $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i]))
if it detects in first condition it is not set or is not an array, it will not proceed to array_key_exists function.
The error probably is, because the incoming data has not indexes like 0,1,2,3... - may be it starts from 1.
The easest solution I think is if you replaces FOR structures by FOREACH.

php for loop variable names

i got a code of 100-200 rules for making a table. but the whole time is happening the same.
i got a variable $xm3, then i make a column . next row, i got $xm2 and make column. next row, i got $xm1 and make column.
so my variables are going to $xm3, $xm2, $xm1, $xm0, $xp1, $xp2, $xp3.
is there a way to make a forloop so i can fill $xm and after that a value from the for loop?
In this kind of structure you'd be better off using an array for these kinds of values, but if you want to make a loop to go through them:
for($i = 0; $i <= 3; $i++) {
$var = 'xm' . $i
$$var; //make column stuff, first time this will be xm0, then xm1, etc.
}
It is not fully clear what you are asking, but you can do
$xm = 'xm3';
$$xm // same as $xm3
in PHP, so you can loop through variables with similar names. (Which does not mean you should. Using an array is usually a superior alternative.)
As far as I am aware using different variable names is not possible.
However if you uses arrays so as below
$xm[3] = "";
$xm[2] = "";
$xm[1] = "";
$xm[0] = "";
or just $xm[] = "";
Then you can use a for each loop:
foreach($xm as $v) { echo $v; }
Edit: Just Googled and this is possible using variable names but is considered poor practice. Learn and use arrays!
You can do this using variable variables, but usually you're better off doing this sort of thing in an array instead.
If you're positive you want to do it this way, and if 'y' is the value of your counter in the for loop:
${'xm' . $y} = $someValue;
You can easily do something like this:
$base_variable = 'xm';
and then you can make a loop creating on the fly the variables;
for example:
for ($i=0; $i<10; $i++)
{
$def_variable = $base_variable . $i;
$$def_variable = 'value'; //this is equivalent to $xm0 = 'value'
}

Categories