Header Ads

PHP 제어문 정리


$i = 0;
$a = 1;
$b = 2;
$c = 3;

// IF , ELSE
if ($a > $b)
{
 echo "1 > 2";
}
else if ($b > $c)
{
 echo "2 > 3";
}
else if ($c > $a)
{
 echo "3 > 1";
}
else
{
 echo "3 < 1";
}

// WHILE
while($i < 10)
{
 echo $i;
 $i++;
}

// DO WHILE
do
{
 echo $i;
 $i--;
}
while($i > 0);

// FOR
for($i=0; $i<10; $i++)
{
 echo $i;
}

// BREAK
while($i > 0)
{
 if ($i == 5)
 {
  echo "Break! \$i = " . $i;
  break;
 }
 echo $i;
 $i--;
}

// CONTINUE
for($i=0; $i<10; $i++)
{
 if($i%2==0)
 {
  echo " even ";
  continue;
 }
 echo $i; //odd
}

// SWITCH
switch($i%2)
{
 case(0) : 
 echo "\$i is even.";
 break;

 case(1) :
 echo "\$i is odd.";
 break;

 default:
 echo "\$i is not Number.";
}

// FOREACH
$abc = array("a"=>1, "b"=>2, "c"=>3);

foreach($abc as $value)
{
 echo $value . " ";
}

foreach($abc as $key=>$value)
{
 echo $key . " = " . $value . " ";
}

// INCLUDE, REQUIRE
include "test.php";
require "test.php";

No comments

Copyright 2016, minki. All rights reserved. Powered by Blogger.