0

Looping/Pengulangan PHP

Posted by Jujur Sitanggang on 5:14 AM


Bab 9
Looping/Pengulangan

Statemen Looping statements digunakan untuk mengeksekusi blok program yang sama beberapa kali.

Jenis-jenis Looping

  • while
  • do...while
  • for
  • foreach

Statemen while

while (condition)
code to be executed;

Program9-1.php
<html>
<body>
<?php
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>
</body>
</html>

Statemen do...while

do
{
code to be executed;
}
while (condition);

Program9-2.php
<html>
<body>
<?php
$i=0;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<5);
?>
</body>
</html>


Statemen for

for (initialization; condition; increment)
{
  code to be executed;
}

Program9-3.php
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
  echo "Hello World!<br />";
}
?>
</body>
</html>


Statemen foreach

foreach (array as value)
{
    code to be executed;
}

Program9-4.php
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
  echo "Value: " . $value . "<br />";
}
?>
</body>
</html>



0 Comments

Copyright Jujur Soaloon Sitangang Lipan All rights reserved. Theme by Sitanggang. | Bloggerized by Soalparna.