Script Perulangan sangat menjadi kebutuhan di pemrograman dan kali ini saya akan berbagi perulangan di PHP
For
<?php/* example 1 */for ($i = 1; $i <= 10; $i++) {
echo $i;
}/* example 2 */for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}/* example 3 */$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}/* example 4 */for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);?>
For Each
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}// $arr is now array(2, 4, 6, 8)unset($value); // break the reference with the last element?>
While
<?php/* example 1 */$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */}/* example 2 */$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;?>
Semoga bermanfaat :D
Wednesday, 6 July 2016
Subscribe to:
Post Comments (Atom)







0 comments:
Post a Comment