I want to print a series of conditionally modified numbers using a loop.
The logic is that the odd occurrences are printed without modification, but the even occurrences are squared (multiplied by themselves).
for ($i=1; $i < 6 ; $i++) {
echo $i;
for ($j=1; $j==$i ; $j++) {
echo ($j+1) * ($j+1);
}
echo " ";
}
The above code prints: 14 2 3 4 5
.
My desired result (if iterating 1
through 6
) is:
// unmodified
// ↓ ↓ ↓
1 4 3 16 5 36
// ↑ ↑↑ ↑↑
// squared (2, 4, and 6)