for loop - Replace the value of a specific element that is either divisible by 2, 3, or 2 and 3 PHP -
i don't know how change value of element if divisible 2, 3, or 2 , 3. wanted output 2 if divisible 2. 3 if divisible 3. , 1 if divisible 2 , 3.
<?php ( $i = 1 ; $i <= 50 ; $i++ ){ if( $i % 3 == 0 && $i % 2 == 0 ){ echo "1 = "; } else if( $i % 2 == 0){ echo "2 = "; } else if( $i % 3 == 0){ echo "3 = "; } echo $i.', '; } ?>
this output. don't want print numbers divisible 2, 3, , 2 , 3.
1, 2 = 2, 3 = 3, 2 = 4, 5, 1 = 6, 7, 2 = 8, 3 = 9, 2 = 10, 11, 1 = 12, 13, 2 = 14, 3 = 15, 2 = 16, 17, 1 = 18, 19, 2 = 20, 3 = 21, 2 = 22, 23, 1 = 24, 25, 2 = 26, 3 = 27, 2 = 28, 29, 1 = 30, 31, 2 = 32, 3 = 33, 2 = 34, 35, 1 = 36, 37, 2 = 38, 3 = 39, 2 = 40, 41, 1 = 42, 43, 2 = 44, 3 = 45, 2 = 46, 47, 1 = 48, 49, 2 = 50,
i wanted output 1 below!
1, 2, 3, 2, 5, 1, 7, 2, 3, 2, 11, 1, 13, 2, 3, 2, 17, 1, 19, 2, 3, 2, 23, 1, 25, 2, 3, 2, 29, 1, 31, 2, 3, 2, 35, 1, 37, 2, 3, 2, 41, 1, 43, 2, 3, 2, 47, 1, 49, 2,
well, shouldn't print them always.
for ( $i = 1 ; $i <= 50 ; $i++ ) { if( $i % 3 == 0 && $i % 2 == 0 ){ echo "1, "; } else if( $i % 2 == 0){ echo "2, "; } else if( $i % 3 == 0){ echo "3, "; } else { echo $i.', '; } }
Comments
Post a Comment