1
Hello, folks. I’m starting in PHP and am having some problems with this code:
<?php
function linha($semana) {
echo "<tr>";
for ($i= 0; $i <= 6; $i++) {
if (isset($semana[$i])) {
echo "<td>{$semana[$i]}</td>";
} else {
echo "<td></td>";
}
}
echo "</tr>";
}
function calendario() {
$dia = 1;
$semana = array();
while ($dia <= 31) {
array_push($semana, $dia);
if (count($semana) == 7) {
linha($semana);
$semana = array();
}
$dia++;
}
linha($semana);
}
?>
<table border="1">
<tr>
<th>Domingo</th>
<th>Segunda</th>
<th>Terça</th>
<th>Quarta</th>
<th>Quinta</th>
<th>Sexta</th>
<th>Sábado</th>
</tr>
<?php calendario();?>
</table>
As you can see, it results in a calendar (without specifics). I cannot understand the logic of this code. Why use for? Why use the if?
If anyone can help me with the logic of this code I thank you!
Thank you, Marcelo! I am training my logic and this problem was a little difficult to understand, but your explanation helped a lot!
– boomboxarcade