Looping/Pengulangan php




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 komentar:

Post a Comment

Copyright © 2014 Dunia Naeta All Right Reserved