๊ฐœ๋ฐœ๊ด€๋ จ/PHP

[PHP]๋ฐ˜๋ณต๋ฌธ(for, while, foreach)์œผ๋กœ ๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐ!

๐Ÿป๊ณฐ์ด๐Ÿป 2020. 12. 3.
728x90

์•ˆ๋…•ํ•˜์„ธ์š” ๊ณฐ์ด์—์š”:)

 

์˜ค๋Š˜์€ ๋ฐ˜๋ณต๋ฌธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ๊ฒ๋‹ˆ๋‹ค.

 

์•Œ์•„๋ณด๋ฉด์„œ~ ๊ตฌ๊ตฌ๋‹จ๋„ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š” ใ…Žใ…Ž

 

์ž ์šฐ์„ FOR๋ฌธ์ž…๋‹ˆ๋‹ค.

 

//for๋ฌธ ์‚ฌ์šฉ๋ฒ•
for($์ดˆ๊ธฐ๋ณ€์ˆ˜;$์กฐ๊ฑด์‹;$์ฆ๊ฐ์‹) 
{
}

for๋ฌธ์€ ์ดˆ๊ธฐ๊ฐ’ ์กฐ๊ฑด๊ฐ’ ์ฆ๊ฐ๊ฐ’์ด ์žˆ๋Š”๋ฐ 

์ดˆ๊ธฐ๊ฐ’ : ๊ธฐ์ค€์ด๋˜๋Š”? ๋ณ€์ˆ˜๊ฐ’.

์กฐ๊ฑด์‹ : ์ดˆ๊ธฐ๊ฐ’์„ ์–ผ๋งˆ๋‚˜ ๋ฐ˜๋ณต์„ ํ• ๊ฑด์ง€ ์กฐ๊ฑด.

์ฆ๊ฐ์‹ : ์กฐ๊ฑด์‹์— ๋งž๋Š” ๋”ํ• ์ง€(+) ๋บผ์ง€(-) ๋ฅผ ์„ ์–ธ.

 

0์—์„œ 10๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š” FOR๋ฌธ์„ ๋งŒ๋“ค๊ณ ์‹ถ์–ด์š”.

for($i=0;$i<=10;$i++) 
{
	echo $i . "<br>";
}

์ดˆ๊ธฐ๊ฐ’์„ 0์œผ๋กœ ์ค๋‹ˆ๋‹ค. 0์—์„œ๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ๊นŒ์š”^^

๊ทธ๋ฆฌ๊ณ  ์กฐ๊ฑด์‹์—๋Š” $i๋ผ๋Š” ๋ณ€์ˆ˜ ์ดˆ๊ธฐ๊ฐ’์ด 10๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„๋•Œ๊นŒ์ง€~~ ๋ฐ˜๋ณต์„ํ• ๊ฑฐ๋‹ค~

๋งˆ์ง€๋ง‰ ์ฆ๊ฐ์‹์—์„œ๋Š” ์ฒ˜๋ฆฌ๋ฅผํ•˜๊ณ  ๋”ํ• ๊ฒƒ์ด๋‹ค. ++์€ ๋”ํ•˜๊ณ  -- ๋Š” ๋บด๋Š”๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

๊ฒฐ๊ณผ

0
1
2
3
4
5
6
7
8
9
10

 

๊ทธ๋Ÿฌ๋ฉด for๋ฌธ์„ ๊ตฌ๊ตฌ๋‹จ์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

for๋ฌธ์•ˆ์— for๋ฌธ์„ ๋˜ ๋„ฃ์–ด์„œ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

<?
	//for๋ฌธ ์‚ฌ์šฉ๋ฒ•
	for($i=1;$i<=9;$i++) 
	{
		echo "[". $i . "] ๋‹จ <br>";
		for($j=1;$j<=9;$j++)
		{
			echo $i . " X " . $j . "=" . ($i*$j) . "<br>";
		}
	}
?>

๊ฒฐ๊ณผ

 

[1] ๋‹จ
1 X 1=1
1 X 2=2
1 X 3=3
1 X 4=4
1 X 5=5
1 X 6=6
1 X 7=7
1 X 8=8
1 X 9=9
[2] ๋‹จ
2 X 1=2
2 X 2=4
2 X 3=6
2 X 4=8
2 X 5=10
2 X 6=12
2 X 7=14
2 X 8=16
2 X 9=18
[3] ๋‹จ...4๋‹จ ์ญ‰์ญ‰ ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

 while๋ฌธ ์‚ฌ์šฉ๋ฒ•

 

<?
	//while๋ฌธ ์‚ฌ์šฉ๋ฒ•
	$i = $๊ธฐ์ค€๋ณ€์ˆ˜;
	while($์กฐ๊ฑด์‹)
	{
		$์ฆ๊ฐ์‹;
	}
?>

while๋ฌธ 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ถœ๋ ฅ

<?
	//while๋ฌธ ์‚ฌ์šฉ๋ฒ•
	$i = 1;
	while($i<=9)
	{
		echo $i ."<br>";
		$i++;
	}
?>

๊ฒฐ๊ณผ

1
2
3
4
5
6
7
8
9

 

๊ตฌ๊ตฌ๋‹จ

<?
	//while๋ฌธ ์‚ฌ์šฉ๋ฒ•
	$i = 1;
	while($i<10)
	{
		$j = 2;
		echo "[" . $i . "] ๋‹จ <br>";
		while($j<10)
		{
			echo $i . " X " . $j . "=" . ($i*$j) . "<br>";
			$j++;
		}

		$i++;
	}
?>

๊ฒฐ๊ณผ๋Š” FOR๋ฌธ์—์„œ์™€ ๊ฐ™์ด ๊ฐ’์€ ๊ฐ™์ง€๋งŒ ๋ฌธ๋ฒ•์— ์ฐจ์ด๊ฐ€์žˆ์Šต๋‹ˆ๋‹ค.

 

[2] ๋‹จ
2 X 1=2
2 X 2=4
2 X 3=6
2 X 4=8
2 X 5=10


2 X 6=12
2 X 7=14
2 X 8=16
2 X 9=18

foreach ์‚ฌ์šฉ๋ฒ•

foreach($๋ฐฐ์—ด as $key=>$val)
{
  echo $key;
  echo $val;
}
<?
	//foreach ์‚ฌ์šฉ๋ฒ•
	$a = array("1", "2", "3", "4", "5", "6", "7", "8", "9");

	foreach($a as $key=>$val)
	{
		echo "KEY ๊ฐ’ : ". $key . "VAL ๊ฐ’ : " . $val ."<br>";
	}
?>

๋ฐฐ์—ด(array)์˜ key๊ฐฏ์ˆ˜๋งŒํผ? ๋ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ ๊ฒƒ๊ฐ™์•„์š”!

 

๊ฒฐ๊ณผ

KEY ๊ฐ’ : 0VAL ๊ฐ’ : 1
KEY ๊ฐ’ : 1VAL ๊ฐ’ : 2
KEY ๊ฐ’ : 2VAL ๊ฐ’ : 3
KEY ๊ฐ’ : 3VAL ๊ฐ’ : 4
KEY ๊ฐ’ : 4VAL ๊ฐ’ : 5
KEY ๊ฐ’ : 5VAL ๊ฐ’ : 6
KEY ๊ฐ’ : 6VAL ๊ฐ’ : 7
KEY ๊ฐ’ : 7VAL ๊ฐ’ : 8
KEY ๊ฐ’ : 8VAL ๊ฐ’ : 9

 

๊ตฌ๊ตฌ๋‹จํ•˜๋ ค๋ฉด $val๊ฐ’์„ ์จ์•ผ๊ฒ ๋„ค์—ฌ ใ…‹ใ…‹

 

ํ‚ค๊ฐ€ ์„ค์ •๋˜์–ด์žˆ์ง€์•Š์œผ๋ฉด 0๋ถ€ํ„ฐ๊ฐ€๋‹ˆ๊นŒ

 

๊ตฌ๊ตฌ๋‹จ

<?
	//foreach ์‚ฌ์šฉ๋ฒ•
	$a = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
	$b = array("1", "2", "3", "4", "5", "6", "7", "8", "9");

	foreach($a as $key=>$val)
	{
		echo "[" . $val . "] ๋‹จ <br>";
		foreach($b as $key2=>$val2)
		{
			echo $val . " X " . $val2 . " = " . ($val*$val2) . "<br>";
		}
	}
?>

๊ฒฐ๊ณผ

 

[2] ๋‹จ
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
[3] ๋‹จ
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27

 

๋˜‘๊ฐ™์ด ๋‚˜์˜ค๋„ค์š”! ๋ฐ˜๋ณต๋ฌธ ๋ฌธ๋ฒ•์ด ์กฐ๊ธˆ ๋‹ฌ๋ผ์„œ๊ทธ๋ ‡์ง€ ์ƒํ™ฉ์—๋งž๊ฒŒ ๋น„๊ตํ•˜๋ฉด์„œ ์“ฐ์‹œ๋ฉด ์ข‹์„๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์˜ค๋Š˜๋„ ์ข‹์€ํ•˜๋ฃจ ๋˜์‹œ๊ตฌ์—ฌ ๋‹ค์Œ์— ๋” ์ข‹์€ ๋‚ด์šฉ์œผ๋กœ ์ฐพ์•„๋ต๊ฒŒ์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€