PHP Variable From A Variable?

Is there any way to make a variable from a variable?  I want it to dynamically create it for me so when I loop through the exact same code, it will get all 365 days for me.  So the desired end result will be:

1, 2, 3, 4....365 for $day365
daily1reading, daily2reading, daily3reading, daily4reading...daily365reading for $dailyreading

The $day365 works great, but the $dailyreading doesn't work, it always echos "$daily1reading" instead of daily1reading

any ideas would be appreciated!
<?php $day365 = 0; ?>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>

Open in new window

LVL 1
bschwartingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AphXCommented:
What's not working with the $dailyreading variable?

It's easier if you attach all of the code.
<?php
 
$day[];
 
for($i = 0; $i <365; $i++){
    echo "<td>";
    $day[$i] = $day[$i] + 1; $dailyreading = $daily . $day[$i] . reading; echo $dailyreading;
    echo $day[$i];
   echo "</td>";
}
 
?>

Open in new window

0
AphXCommented:
<?php

$day[];
 
for($i=0; $i<365; $i++){
       echo "<td>";
       $day[$i] = $day[$i] + 1;
       $dailyreading = $daily . $day[$i] . reading;
       echo $dailyreading;
       echo $day[$i];
       echo "</td>";
}
 
?>
0
AphXCommented:

<?php
 
$day[];
 
for($i=0; $i<365; $i++){
       $day[$i] = $i;
       echo "<td>";
       $day[$i] = $day[$i] + 1;
       $dailyreading = $daily . $day[$i] . reading;
       echo $dailyreading;
       echo $day[$i];
       echo "</td>";
}
 
?>

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

AphXCommented:
Sorry, remove the line 8 like this:
<?php
 
$day[];
 
for($i=0; $i<365; $i++){
       $day[$i] = $i;
       echo "<td>";
       $dailyreading = $daily . $day[$i] . reading;
       echo $dailyreading;
       echo $day[$i];
       echo "</td>";
}
 
?>

Open in new window

0
bschwartingAuthor Commented:
sorry, forgot to include the variables from the top.  it will echo the variable name instead of the variable result.



$daily1reading = 'Here is your reading for January 1st';
$daily2reading = 'Here is your reading for January 2nd';
$daily3reading = 'Here is your reading for January 3rd';
$daily4reading = 'Here is your reading for January 4th';
...
$daily365reading = 'Here is your reading for January 5th';

<?php $day365 = 0; ?>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>


0
bschwartingAuthor Commented:
so when you echo $daily1reading it echos "$daily1reading" instead of "Here is your reading for January 1st"
0
bschwartingAuthor Commented:
sorry, when you echo $dailyreading it echos "$daily1reading" instead of "Here is your reading for January 1st"
0
8riaNCommented:
I'm not sure what you're trying to accomplish.  To produce the output you seem to be looking for above, I'd use this loop:

for ($day=1; $day<=365 ; $day++) {
      echo "<td>daily".$day."reading".$day."</td>";
}

Is there some reason you need these variables later? If so is there a reason you can't use an array?
0
AphXCommented:
Try this code...

It's not complete but it's a start of what I think you need...

Give me feedback please
<?
      function ordinalize($number) {
      if (in_array(($number % 100),range(11,13))){
      return $number.'th';
      }else{
      switch (($number % 10)) {
      case 1:
      return $number.'st';
      break;
      case 2:
      return $number.'nd';
      break;
      case 3:
      return $number.'rd';
      default:
      return $number.'th';
      break;
      }
      }
      }
	  
	  
	  $dailyreading;
	  // Define January
	  for($i=1; $i<32; $i++){
	  $dailyreading[$i] = 'Here is your reading for January '.ordinalize($i);
	  echo $dailyreading[$i];
	  echo "<br />";
	  }
?>

Open in new window

0
Roger BaklundCommented:
You can create variable names dynamically, and use double $$ to resolve it, like this:

$daily1reading = 'Here is your reading for January 1st';
$day365 = 1;
$var = "daily{$day365}reading";
$dailyreading = $$var;
echo $dailyreading;

...but it would probably be better to use a loop, similar to what AphX and 8riaN have suggested. Could you explain in more detail what you want to achieve?
0
Roger BaklundCommented:
You don't need the intermediate variable $var:

$dailyreading = "daily{$day365}reading";
echo $$dailyreading;
0
8riaNCommented:
I'd do this:
<table>
<?php
$thisyear = date('Y',strtotime('Now'));
if (cal_days_in_month(CAL_GREGORIAN, 2, $thisyear) == 29) {$numdays=366;} else {$numdays=365;}
for ($daynum=1; $daynum <= $numdays ; ++$daynum ) {
	$newday = mktime(0,0,0,1,$daynum,$thisyear);
	echo "<tr><td>Here is your reading for ".date('F jS',$newday)."</td></tr>";
}
?>
</table>

Open in new window

0
8riaNCommented:
And just remove all the $daily1reading variables.
0
bschwartingAuthor Commented:
reading through all this now, just a minute guys and thanks for all this!
0
bschwartingAuthor Commented:
ok, what cxr said did what I wanted perfectly.  now i just need a simple loop of this code for 365 days.  how is best to do that for this code?

<td>
<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?>
</td>
$loop365 = 0;
if $loop365 < 365
$loop365 = $loop365 + 1;
echo "<td>
<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?>
</td>"
} else {
echo "<input type="submit" value="Update All 365 Days Now!"><input type="Reset">";

Open in new window

0
Roger BaklundCommented:
Something like this, maybe... though it would be easier to read if you split it on multiple lines. You would need to insert <tr></tr> tags for every x days. Again, if you explain what you want to achieve, it would be easier to give you good advice.
for($loop365=1;$loop365<=365;$loop365++) {
  $dailyreading = "daily{$day365}reading"; 
  echo '<td>'.$$dailyreading.'</td>';
}

Open in new window

0
Roger BaklundCommented:
sorry, wrong variable in the loop:
for($loop365=1;$loop365<=365;$loop365++) {
  $dailyreading = "daily{$loop365}reading"; 
  echo '<td>'.$$dailyreading.'</td>';
}

Open in new window

0
bschwartingAuthor Commented:
i am writing out a table that is 13x29, with a checkbox for the user to select if they have completed that days task.  beside each checkbox is the day, 1-365. (see attached image) i will also need to put in the </tr><tr> like you said every 13 runs of the loop to start the new row.  right now I have the attached code copy/pasted 365 times!  this loop will help when i figure it out.
<td align="center">
<span title="<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?></span>
</td>
<td align="center">
<input type='checkbox' value='1' name='<?php echo answer . $day365; ?>'>
</td>

Open in new window

365-days.png
0
bschwartingAuthor Commented:
also, when you hover over each number the title will pop up with the $daily1reading - $daily365reading text.
0
Roger BaklundCommented:
How does this work:
<?php
echo '<table><tr>';
for($loop365=1;$loop365<=365;$loop365++) {
  if(($loop365-1)%13==0) echo '</tr><tr>'; ?>
  <td align="center"><span title="<?php 
  echo "Daily reading for ".date('F jS',mktime(0,0,0,1,$loop365)); 
?>"><?php echo $loop365; ?></span></td>
<td align="center">
<input type='checkbox' value='1' name="answer<?php echo $loop365; ?>" />
</td><?php
}
echo '</tr></table>';
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bschwartingAuthor Commented:
perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.