From 4e7f0ce330914cded8be937b8e5f1700350623d2 Mon Sep 17 00:00:00 2001 From: wikibook Date: Mon, 19 Aug 2013 17:24:12 +0900 Subject: added korean version of coffeescript and php tutorials --- ko-kr/php-kr.html.markdown | 662 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 662 insertions(+) create mode 100644 ko-kr/php-kr.html.markdown (limited to 'ko-kr/php-kr.html.markdown') diff --git a/ko-kr/php-kr.html.markdown b/ko-kr/php-kr.html.markdown new file mode 100644 index 00000000..2382a8fb --- /dev/null +++ b/ko-kr/php-kr.html.markdown @@ -0,0 +1,662 @@ +--- +language: php +category: language +contributors: + - ["Malcolm Fell", "http://emarref.net/"] + - ["Trismegiste", "https://github.com/Trismegiste"] +filename: learnphp.php +translators: + - ["wikibook", "http://wikibook.co.kr"] +lang: ko-kr +--- + +이 문서에서는 PHP 5+를 설명합니다. + + +```php + +Hello World Again! + 12 +$int2 = -12; // => -12 +$int3 = 012; // => 10 (a leading 0 denotes an octal number) +$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) + +// Float (doubles로도 알려짐) +$float = 1.234; +$float = 1.2e3; +$float = 7E-10; + +// 산술 연산 +$sum = 1 + 1; // 2 +$difference = 2 - 1; // 1 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 + +// 축약형 산술 연산 +$number = 0; +$number += 1; // $number를 1만큼 증가 +echo $number++; // 1을 출력(평가 후 증가) +echo ++$number; // 3 (평가 전 증가) +$number /= $float; // 나눗셈 후 몫을 $number에 할당 + +// 문자열은 작은따옴표로 감싸야 합니다. +$sgl_quotes = '$String'; // => '$String' + +// 다른 변수를 포함할 때를 제외하면 큰따옴표 사용을 자제합니다. +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' + +// 특수 문자는 큰따옴표에서만 이스케이프됩니다. +$escaped = "This contains a \t tab character."; +$unescaped = 'This just contains a slash and a t: \t'; + +// 필요할 경우 변수를 중괄호로 감쌉니다. +$money = "I have $${number} in the bank."; + +// PHP 5.3부터는 여러 줄 문자열을 생성하는 데 나우닥(nowdoc)을 사용할 수 있습니다. +$nowdoc = <<<'END' +Multi line +string +END; + +// 히어닥(heredoc)에서는 문자열 치환을 지원합니다. +$heredoc = << 1, 'Two' => 2, 'Three' => 3); + +// PHP 5.4에서는 새로운 문법이 도입됐습니다. +$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; + +echo $associative['One']; // 1을 출력 + +// 리스트 리터럴은 암시적으로 정수형 키를 할당합니다. +$array = ['One', 'Two', 'Three']; +echo $array[0]; // => "One" + + +/******************************** + * 출력 + */ + +echo('Hello World!'); +// 표준출력(stdout)에 Hello World!를 출력합니다. +// 브라우저에서 실행할 경우 표준출력은 웹 페이지입니다. + +print('Hello World!'); // echo과 동일 + +// echo는 실제로 언어 구성물에 해당하므로, 괄호를 생략할 수 있습니다. +echo 'Hello World!'; +print 'Hello World!'; // 똑같이 출력됩니다. + +$paragraph = 'paragraph'; + +echo 100; // 스칼라 변수는 곧바로 출력합니다. +echo $paragraph; // 또는 변수의 값을 출력합니다. + +// 축약형 여는 태그를 설정하거나 PHP 버전이 5.4.0 이상이면 +// 축약된 echo 문법을 사용할 수 있습니다. +?> +

+ 2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 + + +/******************************** + * 로직 + */ +$a = 0; +$b = '0'; +$c = '1'; +$d = '1'; + +// assert는 인자가 참이 아닌 경우 경고를 출력합니다. + +// 다음과 같은 비교는 항상 참이며, 타입이 같지 않더라도 마찬가지입니다. +assert($a == $b); // 동일성 검사 +assert($c != $a); // 불일치성 검사 +assert($c <> $a); // 또 다른 불일치성 검사 +assert($a < $c); +assert($c > $b); +assert($a <= $b); +assert($c >= $d); + +// 다음과 같은 코드는 값과 타입이 모두 일치하는 경우에만 참입니다. +assert($c === $d); +assert($a !== $d); +assert(1 == '1'); +assert(1 !== '1'); + +// 변수는 어떻게 사용하느냐 따라 다른 타입으로 변환될 수 있습니다. + +$integer = 1; +echo $integer + $integer; // => 2 + +$string = '1'; +echo $string + $string; // => 2 (문자열이 강제로 정수로 변환됩니다) + +$string = 'one'; +echo $string + $string; // => 0 +// + 연산자는 'one'이라는 문자열을 숫자로 형변환할 수 없기 때문에 0이 출력됩니다. + +// 한 변수를 다른 타입으로 처리하는 데 형변환을 사용할 수 있습니다. + +$boolean = (boolean) 1; // => true + +$zero = 0; +$boolean = (boolean) $zero; // => false + +// 대다수의 타입을 형변환하는 데 사용하는 전용 함수도 있습니다. +$integer = 5; +$string = strval($integer); + +$var = null; // 널 타입 + + +/******************************** + * 제어 구조 + */ + +if (true) { + print 'I get printed'; +} + +if (false) { + print 'I don\'t'; +} else { + print 'I get printed'; +} + +if (false) { + print 'Does not get printed'; +} elseif(true) { + print 'Does'; +} + +// 사항 연산자 +print (false ? 'Does not get printed' : 'Does'); + +$x = 0; +if ($x === '0') { + print 'Does not print'; +} elseif($x == '1') { + print 'Does not print'; +} else { + print 'Does print'; +} + + + +// 다음과 같은 문법은 템플릿에 유용합니다. +?> + + +This is displayed if the test is truthy. + +This is displayed otherwise. + + + 2, 'car' => 4]; + +// foreach 문은 배영를 순회할 수 있습니다. +foreach ($wheels as $wheel_count) { + echo $wheel_count; +} // "24"를 출력 + +echo "\n"; + +// 키와 값을 동시에 순회할 수 있습니다. +foreach ($wheels as $vehicle => $wheel_count) { + echo "A $vehicle has $wheel_count wheels"; +} + +echo "\n"; + +$i = 0; +while ($i < 5) { + if ($i === 3) { + break; // while 문을 빠져나옴 + } + echo $i++; +} // "012"를 출력 + +for ($i = 0; $i < 5; $i++) { + if ($i === 3) { + continue; // 이번 순회를 생략 + } + echo $i; +} // "0124"를 출력 + + +/******************************** + * 함수 + */ + +// "function"으로 함수를 정의합니다. +function my_function () { + return 'Hello'; +} + +echo my_function(); // => "Hello" + +// 유효한 함수명은 문자나 밑줄로 시작하고, 이어서 +// 임의 개수의 문자나 숫자, 밑줄이 옵니다. + +function add ($x, $y = 1) { // $y는 선택사항이고 기본값은 1입니다. + $result = $x + $y; + return $result; +} + +echo add(4); // => 5 +echo add(4, 2); // => 6 + +// 함수 밖에서는 $result에 접근할 수 없습니다. +// print $result; // 이 코드를 실행하면 경고가 출력됩니다. + +// PHP 5.3부터는 익명 함수를 선언할 수 있습니다. +$inc = function ($x) { + return $x + 1; +}; + +echo $inc(2); // => 3 + +function foo ($x, $y, $z) { + echo "$x - $y - $z"; +} + +// 함수에서는 함수를 반환할 수 있습니다. +function bar ($x, $y) { + // 'use'를 이용해 바깥 함수의 변수를 전달합니다. + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; +} + +$bar = bar('A', 'B'); +$bar('C'); // "A - B - C"를 출력 + +// 문자열을 이용해 이름이 지정된 함수를 호출할 수 있습니다. +$function_name = 'add'; +echo $function_name(1, 2); // => 3 +// 프로그램 방식으로 어느 함수를 실행할지 결정할 때 유용합니다. +// 아니면 call_user_func(callable $callback [, $parameter [, ... ]]);를 사용해도 됩니다. + +/******************************** + * 인클루드 + */ + +instanceProp = $instanceProp; + } + + // 메서드는 클래스 안의 함수로서 선언됩니다. + public function myMethod() + { + print 'MyClass'; + } + + final function youCannotOverrideMe() + { + } + + public static function myStaticMethod() + { + print 'I am static'; + } +} + +echo MyClass::MY_CONST; // 'value' 출력 +echo MyClass::$staticVar; // 'static' 출력 +MyClass::myStaticMethod(); // 'I am static' 출력 + +// new를 사용해 클래스를 인스턴스화합니다. +$my_class = new MyClass('An instance property'); +// 인자를 전달하지 않을 경우 괄호를 생략할 수 있습니다. + +// ->를 이용해 클래스 멤버에 접근합니다 +echo $my_class->property; // => "public" +echo $my_class->instanceProp; // => "An instance property" +$my_class->myMethod(); // => "MyClass" + + +// "extends"를 이용해 클래스를 확장합니다. +class MyOtherClass extends MyClass +{ + function printProtectedProperty() + { + echo $this->prot; + } + + // 메서드 재정의 + function myMethod() + { + parent::myMethod(); + print ' > MyOtherClass'; + } +} + +$my_other_class = new MyOtherClass('Instance prop'); +$my_other_class->printProtectedProperty(); // => "protected" 출력 +$my_other_class->myMethod(); // "MyClass > MyOtherClass" 출력 + +final class YouCannotExtendMe +{ +} + +// "마법 메서드(magic method)"로 설정자 메서드와 접근자 메서드를 만들 수 있습니다. +class MyMapClass +{ + private $property; + + public function __get($key) + { + return $this->$key; + } + + public function __set($key, $value) + { + $this->$key = $value; + } +} + +$x = new MyMapClass(); +echo $x->property; // __get() 메서드를 사용 +$x->property = 'Something'; // __set() 메서드를 사용 + +// 클래스는 추상화하거나(abstract 키워드를 사용해) +// 인터페이스를 구현할 수 있습니다(implments 키워드를 사용해). +// 인터페이스는 interface 키워드로 선언합니다. + +interface InterfaceOne +{ + public function doSomething(); +} + +interface InterfaceTwo +{ + public function doSomethingElse(); +} + +// 인터페이스는 확장할 수 있습니다. +interface InterfaceThree extends InterfaceTwo +{ + public function doAnotherContract(); +} + +abstract class MyAbstractClass implements InterfaceOne +{ + public $x = 'doSomething'; +} + +class MyConcreteClass extends MyAbstractClass implements InterfaceTwo +{ + public function doSomething() + { + echo $x; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } +} + + +// 클래스에서는 하나 이상의 인터페이스를 구현할 수 있습니다. +class SomeOtherClass implements InterfaceOne, InterfaceTwo +{ + public function doSomething() + { + echo 'doSomething'; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } +} + + +/******************************** + * 특성 + */ + +// 특성(trait)은 PHP 5.4.0부터 사용 가능하며, "trait"으로 선언합니다. + +trait MyTrait +{ + public function myTraitMethod() + { + print 'I have MyTrait'; + } +} + +class MyTraitfulClass +{ + use MyTrait; +} + +$cls = new MyTraitfulClass(); +$cls->myTraitMethod(); // "I have MyTrait"을 출력 + + +/******************************** + * 네임스페이스 + */ + +// 이 부분은 별도의 영역인데, 파일에서 처음으로 나타나는 문장은 +// 네임스페이스 선언이어야 하기 때문입니다. 여기서는 그런 경우가 아니라고 가정합니다. + +