Not getting dynamic variable names PHP - php

I want to print the values of variable but it prints 12 only
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}

This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>

Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}

Related

Naming PHP object children in for loop

I want to do this:
<?php echo $factuurgegevens->aantal.$i; ?>
But it doesn't work, how can I handle this?
So I have to get:
<?php echo $factuurgegevens->aantal1; ?>
<?php echo $factuurgegevens->aantal2; ?>
<?php echo $factuurgegevens->aantal3; ?>
<?php echo $factuurgegevens->aantal4; ?>
You can use curly braces ({}) to make up the dynamic variables:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
However you should really use an array because that is made for things like this:
$factuurgegevens->aantal = array();
$factuurgegevens->aantal[1] = 'something';
$factuurgegevens->aantal[2] = 'something';
$factuurgegevens->aantal[3] = 'something';
Do this:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
You can do this by placing your string in a variable.
for($i=0;$i<10;$i++)
{
$var = 'aantal'.$i;
echo $factuurgegevens->$var;
}

PHP Table Loop?

<?php
$i=0;
while($i < 101){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
?>
This code generates a table with 100 rows and 2 columns. But what I want to do is that show ordered numbers (upp to 100) in the left side of the rowcells and show something else (ex. pow(rownumber) ) in the right side of the rowcells. How can I do that?
Try this, Will output 100 rows with the number and its power in two columns
<table>
<?php
for($i = 0; $i <= 100; $i++){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>',
$i,
pow($i, 2)
);
}
?>
</table>
Is this what you are looking for?
for($i=0; i<100; i++){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>Something Else</td>".PHP_EOL;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
You can use your modulus (and a for loop too)
for ($i = 1; $i <= 100; $i++){
$mod = ($i%2==0) ? true : false;
if($mod) echo "<tr>".PHP_EOL;
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>". foo($bar) ."</td>".PHP_EOL;
if($mod) echo "</tr>".PHP_EOL;
}
<?php
$i=0;
$j=0;
while($i < 101){
if($i%2==0){
$j++;
echo "<tr>"."<td>".$j."</td>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "<td>any text</td>"."</tr>".PHP_EOL;
}
}
?>
Why use modulo for only two options? This solution seems much easier. Your $data is an array of the things you want to display, currently the alphabet.
$data = range('a','z');
foreach($data as $num => $elem) {
echo "<tr>".PHP_EOL;
echo "<td>".$num.</td>".PHP_EOL;
echo "<td>".$elem.</td>".PHP_EOL;
echo "</tr>".PHP_EOL;
}
If you want to make it loop 100+ times, just make the array that size.
Foreach documentation

PHP variables in a complex way

<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}

function and not using Global

Hi I was wondering if someone could help me out. Ive tried Global but this is not what I want.
$i= 0
if(.....){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
function process(){
echo $i;
}
Wouldn't it be better to just pass the stuff you need inside the method, as argument?
process($i);
function process($i){
echo $i;
}
Inside the function also you should declare the global variable.
function process(){
global $i;
echo $i;
}
I tested this and it worked for me the way i believe you would like it to work
$i= 0;
if(1==2){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
function process(){
global $i;
echo $i;
}
process();
I think you are defining the function and not calling it after the if-else block. you can define it anywhere but call this function after the if-else block simply like
if(....){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
process();
Sure it will work with $_GLOBALS. Try this code by playing with $var variable
<?php
$i= 0;
$var=0;
if($var==0){
echo "In this part";
$GLOBALS["i"]=1;
}
else{
echo "........";
$GLOBALS["i"]=2;
}
process();
function process(){
echo $GLOBALS["i"];
}
?>

PHP loop in object

I have 4 instances of the same object:
echo $payment->field_course_1_length[0]['value'];
echo $payment->field_course_2_length[0]['value'];
echo $payment->field_course_3_length[0]['value'];
echo $payment->field_course_4_length[0]['value'];
My question is so that I don't have to be typing the same over and over can I put it in a loop like this:
for ($i=1; $i<5; $i++) {
echo $payment->field_course_'$i'_length[0]['value'];
}
Thank you.
echo $payment->{"field_course_{$i}_length"}[0]['value'];
$tmp = $payment->{'field_course_' . $i . '_length'};
echo $tmp[0]['value'];
However, I strongly recommend to use arrays instead of dynamic properties. If they are not dynamic, don't access them dynamic.
You can do like this:
for($i = 1; $i <= 4; $i++) {
echo $payment->{"field_course_".$i."_length"}[0]['value'];
}

Categories