cvs: phpdoc /kr/language types.xml

From: Date: Tue, 09 Jan 2001 06:04:52 +0000
Subject: cvs: phpdoc /kr/language types.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969333497@lists.php.net to get a copy of this message
regina Mon Jan 8 22:04:52 2001 EDT Added files: /phpdoc/kr/language types.xml Log:

Index: phpdoc/kr/language/types.xml +++ phpdoc/kr/language/types.xml <chapter id="language.types"> <title>변수 형태 (Types)</title> <para> PHP는 다음 형태의 type을 지원한다. : <itemizedlist> <listitem> <simpara> <link linkend="language.types.array">배열(array)</link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.double">실수형(floating-point numbers)</link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.integer">정수형(integer)</link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.object">객체(object)</link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.string">문자열(string)</link> </simpara> </listitem> </itemizedlist> </para> <simpara> 변수타입은 보통 프로그래머가 설정해 줄 필요가 없다. 대신 PHP가 실행중에 변수 값의 내용에 따라 자동으로 바꿔가며 설정한다. </simpara> <simpara> 만약 여러분이 어떤 변수를 특정한 변수 타입으로 지정하고 싶다면 해당 변수를 <link linkend="language.types.typecasting">cast</link> 하거나, <function>settype</function> 함수를 사용한다. </simpara> <simpara> 종종 변수들은 실행시 어떤 타입으로 되어 있느냐에 따라 특정한 상황에서 원하는 대로 동작하지 않을 때가 있다. <link linkend="language.types.type-juggling">Type Juggling</link> 편을 보면 자세히 알 수 있다. </simpara> <sect1 id="language.types.integer"> <title>정수형 (Integers)</title> <para> 정수형은 다음과 같은 형태의 명령으로 지정할 수 있다. <informalexample> <programlisting role="php"> $a = 1234; # decimal number $a = -123; # a negative number $a = 0123; # octal number (equivalent to 83 decimal) $a = 0x12; # hexadecimal number (equivalent to 18 decimal) </programlisting> </informalexample> 정수의 크기는 실행환경에따라 다르다.(platform-dependent) 보통 최대값이 약 20억(32 bits signed)인 경우가 많다. </para> </sect1> <sect1 id="language.types.double"> <title>실수형 (Floating point numbers)</title> <para> 실수형("doubles")은 다음과 같은 형태의 문장으로 지정할 수 있다. <informalexample> <programlisting role="php"> $a = 1.234; $a = 1.2e3; </programlisting> </informalexample> 정수의 크기는 실행환경에따라 다르다.(platform-dependent) 보통 최대값이 약 ~1.8e308에 14자리의 정밀도(64 bit IEEE format)인 경우가 많다. </para> <warning id="warn.float-precision"> <para> It is quite usual that simple decimal fractions like <literal>0.1</literal> or <literal>0.7</literal> cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, <literal>floor((0.1+0.7)*10)</literal> will usually return <literal>7</literal> instead of the expected <literal>8</literal> as the result of the internal representation really being something like <literal>7.9999999999...</literal>. </para> <para> This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, <literal>1/3</literal> in decimal form becomes <literal>0.3333333. . .</literal>. </para> <para> So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the <link linkend="ref.bc">arbitrary precision math functions</link> or <link linkend="ref.gmp">gmp</link> functions instead. </para> </warning> </sect1> <sect1 id="language.types.string"> <title>문자열 (Strings)</title> <para> 문자열은 두 개의 delimiters를 사용해 나타낸다. </para> <para> 문자열을 큰 따옴표(")로 둘러 싸게 되면, 특수 문자들은 다음과 같이 표시하여 포함시킨다. C나 Perl에서처럼 백슬래시(\)를 사용하여 특수 기호를 표시하게 된다. <table> <title>Escaped characters</title> <tgroup cols="2"> <thead> <row> <entry>sequence</entry> <entry>meaning</entry> </row> </thead> <tbody> <row> <entry> <literal>\n</literal> </entry> <entry>linefeed (LF or 0x0A in ASCII)</entry> </row> <row> <entry> <literal>\r</literal> </entry> <entry>carriage return (CR or 0x0D in ASCII)</entry> </row> <row> <entry> <literal>\t</literal> </entry> <entry>horizontal tab (HT or 0x09 in ASCII)</entry> </row> <row> <entry> <literal>\\</literal> </entry> <entry>backslash</entry> </row> <row> <entry> <literal>\$</literal> </entry> <entry>dollar sign</entry> </row> <row> <entry> <literal>\"</literal> </entry> <entry>double-quote</entry> </row> <row> <entry> <literal>\[0-7]{1,3}</literal> </entry> <entry> the sequence of characters matching the regular expression is a character in octal notation </entry> </row> <row> <entry> <literal>\x[0-9A-Fa-f]{1,2}</literal> </entry> <entry> the sequence of characters matching the regular expression is a character in hexadecimal notation </entry> </row> </tbody> </tgroup> </table> </para> <para> 위에 나온 것 외의 다른 문자도 escape를 할 수 있지만, warning이 발생하게 된다. </para> <para> 문자열을 표현하는 또 다른 방법은 큰 따옴표(") 대신 작은 따옴표(')를 사용하는 것이다. 이것은 \\과 \'의 두 개만을 백슬래시 처리하고 나머지는 그냥 둔다. 작은 따옴표를 사용하는 경우에는 변수를 문자열 안에 두어 그 변수의 내용을 문자열에 삽입하는 기능을 사용할 수 <emphasis>없다</emphasis>. </para> <simpara> 문자열을 선언하는 다른 방법은 here doc 구문("&lt;&lt;&lt;")을 사용하는 것이다. <literal>&lt;&lt;&lt;</literal> 뒤에 적당한 identifier를 적어주고, 원하는 문자열의 내용을 적은 후에, 앞에 적은 identifier로 문자열을 끝맷으면 된다. 끝맷는 identifier는 <emphasis>반드시</emphasis> 해당 라인의 첫번째 칸에서 시작해야 한다. 해당 레이블의 이름은 PHP의 다른 레이블과 동일한 방식으로 지으면 된다. 영문자와 숫자, 밑줄(_)만을 사용하여야 하고, 첫글자로 숫자는 안된다. </simpara> <para> Here doc 문자열은 큰따옴표를 제외하고는 큰따옴표로 묶인 문자열과 동일하게 취급된다. 이것은 Here doc 문자열내에서는 큰따옴표를 escape 할 필요가 없고, 위에 적힌 다른 escape 코드는 사용할 수 있다는 것을 의미한다. 문자열내에 변수를 사용하여 그 변수의 내용을 문자열에 삽입하는 기능을 사용할 수 있지만, 변수가 들어가는 식이 복잡할 경우 주의해서 사용하자. <example> <title>Here doc string quoting example</title> <programlisting> &lt;?php $str = &lt;&lt;&lt;EOD Example of string spanning multiple lines using heredoc syntax. EOD; /* More complex example, with variables. */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo &lt;&lt;&lt;EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; ?> </programlisting> </example> </para> <note> <para> Here doc은 PHP 4에서부터 지원된다 </para> </note> <para> 문자열은 '.' (점) 연산자로 연결할 수 있다. '+' (더하기) 연산자는 사용할 수 없다. 자세한 정보는 <link linkend="language.operators.string">String operators</link> 부분을 참조하기 바란다. </para> <para> 문자열내의 개개의 문자는, C 언어에서의 문자의 배열로된 문자열처럼 배열에 숫자 인덱스를 통해 접근이 가능하다. 아래 그 예가 있다. </para> <para> <example> <title>Some string examples</title> <programlisting role="php"> &lt;?php /* Assigning a string. */ $str = "This is a string"; /* Appending to it. */ $str = $str . " with some more text"; /* Another way to append, includes an escaped newline. */ $str .= " and a newline at the end.\n"; /* This string will end up being '&lt;p&gt;Number: 9&lt;/p&gt;' */ $num = 9; $str = "&lt;p&gt;Number: $num&lt;/p&gt;"; /* This one will be '&lt;p&gt;Number: $num&lt;/p&gt;' */ $num = 9; $str = '&lt;p&gt;Number: $num&lt;/p&gt;'; /* Get the first character of a string */ $str = 'This is a test.'; $first = $str[0]; /* Get the last character of a string. */ $str = 'This is still a test.'; $last = $str[strlen($str)-1]; ?&gt; </programlisting> </example> </para> <sect2 id="language.types.string.conversion"> <title>문자열 변환 (String conversion)</title> <simpara> 문자열이 숫자값로 다루어질 때, 결과 값과 type은 다음과 같이 결정된다. </simpara> <simpara> 만약 문자열 안에 '.' 이나 'e', 'E'의 문자가 있을 경우 그 type은 double이 된다. 그렇지 않다면 정수 값이다 </simpara> <para> 해당하는 값은 문자열의 처음에서 시작해서 가능한 부분까지만 사용된다. 문자열이 올바른 숫자 데이터로 시작하지 않으면 그 값은 0이다. 올바른 숫자 데이터는 '+'/'-', '0'-'9','.'과 숫자 뒤의 'e'나 'E' 표시이다 </para> <simpara> 첫 번째 표현식이 string인 경우, 변수의 type은 첫 번째가 아닌 두 번째 표현식에 의해 결정된다. </simpara> <informalexample> <programlisting role="php"> $foo = 1 + "10.5"; // $foo is double (11.5) $foo = 1 + "-1.3e3"; // $foo is double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is integer (1) $foo = 1 + "bob3"; // $foo is integer (1) $foo = 1 + "10 Small Pigs"; // $foo is integer (11) $foo = 1 + "10 Little Piggies"; // $foo is integer (11) $foo = "10.0 pigs " + 1; // $foo is integer (11) $foo = "10.0 pigs " + 1.0; // $foo is double (11) </programlisting> </informalexample> <simpara> 이 변환에 대한 자세한 설명은Unix manual page의 strtod(3)을 보아라. </simpara> <para> 만약 여러분이 위의 예제를 실제 실험하고자 한다면, 위의 예제를 잘라 붙인 후에, 어떤 일이 일어났는 가를 알기 위해 아래의 줄을 삽입한다. <informalexample> <programlisting role="php"> echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br&gt;\n"; </programlisting> </informalexample> </para> </sect2> </sect1> <sect1 id="language.types.array"> <title>배열(Arrays)</title> <para> 배열을 실제로 hash tables (associative arrays) 과 indexed arrays (vectors)의 두가지로 동시에 사용된다. </para> <sect2 id="language.types.array.single-dim"> <title>일차원 배열 (Single Dimension Arrays)</title> <para> PHP는 scalar 배열과 associative 배열의 두 가지를 지원한다. 사실 두 가지의 차이는 없다. 여러분은 <function>list</function> 나<function>array</function> 함수를 사용하여 배열을 만들거나, 각 원소의 값을 정해 주어 배열을 만들 수 있다. <informalexample> <programlisting role="php"> $a[0] = "abc"; $a[1] = "def"; $b["foo"] = 13; </programlisting> </informalexample> </para> <para> 또한 다음과 같이 변수에 값을 더해주는 것만으로 배열을 만들 수도 있다. 배열 변수에 빈 대괄호([])를 붙이고 값을 지정하면, 해당 배열의 맨뒤에 한 원소가 추가되고 그 값이 이 추가된 원소에 저장된다. <informalexample> <programlisting role="php"> $a[] = "hello"; // $a[2] == "hello" $a[] = "world"; // $a[3] == "world" </programlisting> </informalexample> </para> <para> 배열은 정렬을 원하는 형식에 따라 <function>asort</function>, <function>arsort</function>, <function>ksort</function>, <function>rsort</function>, <function>sort</function>, <function>uasort</function>, <function>usort</function>, <function>uksort</function> 함수들을 이용해 순서대로 정렬할 수 있다. </para> <para> <function>count</function> 함수를 사용하면 배열의 원소 개수를 셀 수 있다. </para> <para> <function>next</function>와 <function>prev</function>함수를 이용하여 배열의 내용을 탐색할 수 있다. 배열의 내용을 탐색하는 방법으로 <function>each</function>함수를 사용할 수도 있다. </para> </sect2> <sect2 id="language.types.array.multi-dim"> <title>다차원 배열 (Multi-Dimensional Arrays)</title> <para> 다차원 배열도 실제로 매우 간단한다. 배열의 각 차원에 대하여 단지 [key]값을 뒤에 붙여주면 된다.: <informalexample> <programlisting role="php"> $a[1] = $f; # one dimensional examples $a["foo"] = $f; $a[1][0] = $f; # two dimensional $a["foo"][2] = $f; # (you can mix numeric and associative indices) $a[3]["bar"] = $f; # (you can mix numeric and associative indices) $a["foo"][4]["bar"][0] = $f; # four dimensional! </programlisting> </informalexample> </para> <para> PHP3에서, 문자열 내에서 다차원 배열의 값을 직접 접근하는 것은 불가능하다. 예를들어 다음과 같은 문장은 원하는 결과를 얻지 못할 것이다.: <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This won't work: $a[3][bar]"; </programlisting> </informalexample> PHP3에서 위의 출력은 <computeroutput>This won't work: Array[bar]</computeroutput> 일 것이다. 원하는 결과를 얻으려면 문자열 연결 연산자(.)를 사용하여야 할 것이다. : <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This will work: " . $a[3][bar]; </programlisting> </informalexample> </para> <para> PHP4에서는 접근하려는 배열식을 중괄호({})로 둘러쌈으로써 이것이 가능하다.: <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This will work: {$a[3][bar]}"; </programlisting> </informalexample> </para> <para> 여러 가지 방법으로 이 다차원 배열에 값을 넣을 수 있으나, associative 배열에 값을 저장하는 간단한 방법으로 <function>array</function> 명령을 사용하는 방법이 있다. 우선 다음의 두가지 코드는 일차원 배열에 값을 저장하는 방법으로 결과는 동일하다.: <informalexample> <programlisting role="php"> # Example 1: $a["color"] = "red"; $a["taste"] = "sweet"; $a["shape"] = "round"; $a["name"] = "apple"; $a[3] = 4; # Example 2: $a = array( "color" => "red", "taste" => "sweet", "shape" => "round", "name" => "apple", 3 => 4 ); </programlisting> </informalexample> </para> <para> <function>array</function> 함수는 다차원 배열에 대해서도 다음과 같이 사용할 수 있다. : <informalexample> <programlisting role="php"> &lt;?php $a = array( "apple" => array( "color" => "red", "taste" => "sweet", "shape" => "round" ), "orange" => array( "color" => "orange", "taste" => "tart", "shape" => "round" ), "banana" => array( "color" => "yellow", "taste" => "paste-y", "shape" => "banana-shaped" ) ); echo $a["apple"]["taste"]; # will output "sweet" ?> </programlisting> </informalexample> </para> </sect2> </sect1> <sect1 id="language.types.object"> <title>객체 (Objects)</title> <sect2 id="language.types.object.init"> <title>객체 초기화 (Object Initialization)</title> <para> 객체(Object)를 초기화하는 방법은 <literal>new</literal> 명령을 사용하여 object를 변수에 인스턴스 시키는 것이다. <informalexample> <programlisting role="php"> &lt;?php class foo { function do_foo() { echo "Doing foo."; } } $bar = new foo; $bar->do_foo(); ?> </programlisting> </informalexample> </para> <simpara> 자세한 논의는 <link linkend="language.oop">클래스와 객체(Classes and Objects)</link> 절을 읽어보기 바란다. </simpara> </sect2> </sect1> <sect1 id="language.types.type-juggling"> <title>Type 전환 (Type juggling)</title> <simpara> PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable <parameter>var</parameter>, <parameter>var</parameter> becomes a string. If you then assign an integer value to <parameter>var</parameter>, it becomes an integer. </simpara> <para> An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a double, then all operands are evaluated as doubles, and the result will be a double. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated. <informalexample> <programlisting role="php"> $foo = "0"; // $foo is string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs"; // $foo is integer (15) </programlisting> </informalexample> </para> <simpara> If the last two examples above seem odd, see <link linkend="language.types.string.conversion">String conversion</link>. </simpara> <simpara> If you wish to force a variable to be evaluated as a certain type, see the section on <link linkend="language.types.typecasting">Type casting</link>. If you wish to change the type of a variable, see <function>settype</function>. </simpara> <para> If you would like to test any of the examples in this section, you can cut and paste the examples and insert the following line to see for yourself what's going on: <informalexample> <programlisting role="php"> echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br&gt;\n"; </programlisting> </informalexample> </para> <note> <para> The behaviour of an automatic conversion to array is currently undefined. <informalexample> <programlisting role="php"> $a = 1; // $a is an integer $a[0] = "f"; // $a becomes an array, with $a[0] holding "f" </programlisting> </informalexample> </para> <para> While the above example may seem like it should clearly result in $a becoming an array, the first element of which is 'f', consider this: <informalexample> <programlisting role="php"> $a = "1"; // $a is a string $a[0] = "f"; // What about string offsets? What happens? </programlisting> </informalexample> </para> <para> Since PHP supports indexing into strings via offsets using the same syntax as array indexing, the example above leads to a problem: should $a become an array with its first element being "f", or should "f" become the first character of the string $a? </para> <para> For this reason, as of PHP 3.0.12 and PHP 4.0b3-RC4, the result of this automatic conversion is considered to be undefined. Fixes are, however, being discussed. </para> </note> <sect2 id="language.types.typecasting"> <title>Type Casting</title> <para> Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. <informalexample> <programlisting role="php"> $foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double </programlisting> </informalexample> </para> <para> The casts allowed are: <itemizedlist> <listitem> <simpara>(int), (integer) - cast to integer</simpara> </listitem> <listitem> <simpara>(real), (double), (float) - cast to double</simpara> </listitem> <listitem> <simpara>(string) - cast to string</simpara> </listitem> <listitem> <simpara>(array) - cast to array</simpara> </listitem> <listitem> <simpara>(object) - cast to object</simpara> </listitem> </itemizedlist> </para> <para> Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: <informalexample> <programlisting role="php"> $foo = (int) $bar; $foo = ( int ) $bar; </programlisting> </informalexample> </para> <para> It may not be obvious exactly what will happen when casting between certain types. For instance, the following should be noted. </para> <para> When casting from a scalar or a string variable to an array, the variable will become the first element of the array: <informalexample> <programlisting role="php"> $var = 'ciao'; $arr = (array) $var; echo $arr[0]; // outputs 'ciao' </programlisting> </informalexample> </para> <para> When casting from a scalar or a string variable to an object, the variable will become an attribute of the object; the attribute name will be 'scalar': <informalexample> <programlisting role="php"> $var = 'ciao'; $obj = (object) $var; echo $obj-&gt;scalar; // outputs 'ciao' </programlisting> </informalexample> </para> </sect2> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->
« previous php.doc (#969333497) next »