I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).
$_POST[Var1]
$_POST[Var2]
$_POST[Var3]
How do I use a for loop to loop through and echo the values in each?
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . '$i'];
}
This does not seem to work.
Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . $i];
}
This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.
Check this. While using php varibles dont put upper commas for the variables.
<?php
$_POST["file1"];
$_POST["file2"];
$_POST["file3"];
for ($i = 1; $i <= 3; $i++){
echo $_POST['file'.$i];
}
Or this way:
$_POST["Var{$i}"];
<?php
$listing_weblinkurl_1z1 = "some1";
$listing_weblinkurl_1z2 = "some2";
$listing_weblinkurl_1z3 = "some3";
$listing_weblinkurl_1z4 = "some4";
for($i=1;$i<=4;$i++){
print ($listing_weblinkurl_1z.$i);
}
?>
It is not working. I know that it is wrong to add $i in variable to call it. But I want it.
This is how it would be done, using variable variables.
for ( $i = 1; $i <= 4; $i+= 1 )
{
$varname = 'listing_weblinkurl_1z' . $i;
echo $$varname;
}
However, this is not a good way of writing code. Instead, $listing_weblinkurl should probably be an array containing keys of 1z1, 1z2, etc.
for($i=1;$i<=4;$i++) {
print ( ${"listing_weblinkurl_1z$i"} );
}
As #McAden pointed out in the comments, it would probably be a better idea to use an array.
I am trying this code:
for ($x = 0; $x < $numCol; $x++) {
for ($i = 0; $i < $numRows; $i++) {
$arr.$x[] = $todas[$i][$x]."\n"; //problem here
}
}
echo $arr0[0];
echo $arr1[0];
...
But i get this warning: Cannot use a scalar value as an array
and the echos do nothing. Why ? and what is the solution ?
Here's what you think you want to do. Replace your //problem here line with:
${'arr' . $x}[] = $todas[$x][$i]."\n";
But I would strongly recommend against doing that. Just use your bidimensional array.
I think you meant: ${'arr'.$x}[] instead of $arr.$x[].
$arr.$x[]
Will concatenate the string representation of $arr and $x together so you end up with something like 'Array0'[] = ... instead of $arr0[]
When you write $arr.$x[], it is equal to $arr[$x][]
Try replacing your echos by
echo $arr[0][0];
echo $arr[1][0];
Im trying to spit out each letter of the alphabet from an array on a single line, A-Z.
This is what my code looks like so far:
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
while ($alphabet) {
echo $alphabet;
$alphabet;
}
Im kinda stuck at this part and not quite sure what else to write to make this work. Any suggestions?
Use range and array_walk:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
Working example: http://codepad.org/pedjOlY9
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach($alphabet as $letter) {
echo $letter;
}
I am not sure why you need the array...
This is why we have the ASCII code.
You can do is like this:
for ($i = 65; $i <=90; $i++)
{
echo chr($i) . PHP_EOL;
}
chr() displays the character in the ASCII map - check it here: http://www.danshort.com/ASCIImap/. If you want to do lowercase - just use strtolower() or the numbers between 97-122 instead. PHP_EOL is a built in constant that outputs end of line. You can change it with ."" if you are doing HTML.
I think range is a little bit longer to be done, but it still works.
This might be help full for you
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$c = sizeof($alphabet);
for($i= 0; $i < $c ; $i++) {
echo $alphabet[$i];
}
and you can use count($alphabet) instead of sizeof() built-in function
Might be an easy question for you guys. can't find it on google.
I am trying to concatenate two variables name;
$i=0;
for ($i=0;$i<5;$i++){
if($array[$i]>0){
$test.$i=//do something
}else{
$test.$i=//do something
}
}
//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!
try ${$test.$i} = value
EDIT: http://php.net/manual/en/language.variables.variable.php
I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:
${"test".$i}
Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.
As an example, instead of:
$test0 = "hello";
$test1 = "world";
Use:
$test[0] = "hello";
$test[1] = "world";
Try this:
for ($i=0;$i<5;$i++){
$the_test = $test.$i;
if($array[$i]>0){
$$the_test=//do something
}
else{
$$the_test=//do something
}
}
This might work:
$varName = $test . $i;
$$varName = ...
Can i ask where for this is neccesary?