cvs: phpdoc /kr/language types.xml
| From: | WonHee Hwang | 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 구문("<<<")ì ì¬ì©íë ê²ì´ë¤. <literal><<<</literal> ë¤ì ì ë¹í identifier를 ì ì´ì£¼ê³ , ìíë 문ìì´ì ë´ì©ì ì ì íì, ìì ì ì identifierë¡ ë¬¸ìì´ì ëë§·ì¼ë©´ ëë¤. ëë§·ë identifierë <emphasis>ë°ëì</emphasis> í´ë¹ ë¼ì¸ì 첫ë²ì§¸ 칸ìì ììí´ì¼ íë¤. í´ë¹ ë ì´ë¸ì ì´ë¦ì PHPì ë¤ë¥¸ ë ì´ë¸ê³¼ ëì¼í ë°©ìì¼ë¡ ì§ì¼ë©´ ëë¤. ì문ìì ì«ì, ë°ì¤(_)ë§ì ì¬ì©íì¬ì¼ íê³ , 첫ê¸ìë¡ ì«ìë ìëë¤. </simpara> <para> Here doc 문ìì´ì í°ë°ì´í를 ì ì¸íê³ ë í°ë°ì´íë¡ ë¬¶ì¸ ë¬¸ìì´ê³¼ ëì¼íê² ì·¨ê¸ëë¤. ì´ê²ì Here doc 문ìì´ë´ììë í°ë°ì´í를 escape í íìê° ìê³ , ìì ì í ë¤ë¥¸ escape ì½ëë ì¬ì©í ì ìë¤ë ê²ì ì미íë¤. 문ìì´ë´ì ë³ì를 ì¬ì©íì¬ ê·¸ ë³ìì ë´ì©ì 문ìì´ì ì½ì íë 기ë¥ì ì¬ì©í ì ìì§ë§, ë³ìê° ë¤ì´ê°ë ìì´ ë³µì¡í ê²½ì° ì£¼ìí´ì ì¬ì©íì. <example> <title>Here doc string quoting example</title> <programlisting> <?php $str = <<<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 <<<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"> <?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 '<p>Number: 9</p>' */ $num = 9; $str = "<p>Number: $num</p>"; /* This one will be '<p>Number: $num</p>' */ $num = 9; $str = '<p>Number: $num</p>'; /* 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]; ?> </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) . "<br>\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"> <?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"> <?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) . "<br>\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->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: -->
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 구문("<<<")ì ì¬ì©íë ê²ì´ë¤. <literal><<<</literal> ë¤ì ì ë¹í identifier를 ì ì´ì£¼ê³ , ìíë 문ìì´ì ë´ì©ì ì ì íì, ìì ì ì identifierë¡ ë¬¸ìì´ì ëë§·ì¼ë©´ ëë¤. ëë§·ë identifierë <emphasis>ë°ëì</emphasis> í´ë¹ ë¼ì¸ì 첫ë²ì§¸ 칸ìì ììí´ì¼ íë¤. í´ë¹ ë ì´ë¸ì ì´ë¦ì PHPì ë¤ë¥¸ ë ì´ë¸ê³¼ ëì¼í ë°©ìì¼ë¡ ì§ì¼ë©´ ëë¤. ì문ìì ì«ì, ë°ì¤(_)ë§ì ì¬ì©íì¬ì¼ íê³ , 첫ê¸ìë¡ ì«ìë ìëë¤. </simpara> <para> Here doc 문ìì´ì í°ë°ì´í를 ì ì¸íê³ ë í°ë°ì´íë¡ ë¬¶ì¸ ë¬¸ìì´ê³¼ ëì¼íê² ì·¨ê¸ëë¤. ì´ê²ì Here doc 문ìì´ë´ììë í°ë°ì´í를 escape í íìê° ìê³ , ìì ì í ë¤ë¥¸ escape ì½ëë ì¬ì©í ì ìë¤ë ê²ì ì미íë¤. 문ìì´ë´ì ë³ì를 ì¬ì©íì¬ ê·¸ ë³ìì ë´ì©ì 문ìì´ì ì½ì íë 기ë¥ì ì¬ì©í ì ìì§ë§, ë³ìê° ë¤ì´ê°ë ìì´ ë³µì¡í ê²½ì° ì£¼ìí´ì ì¬ì©íì. <example> <title>Here doc string quoting example</title> <programlisting> <?php $str = <<<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 <<<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"> <?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 '<p>Number: 9</p>' */ $num = 9; $str = "<p>Number: $num</p>"; /* This one will be '<p>Number: $num</p>' */ $num = 9; $str = '<p>Number: $num</p>'; /* 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]; ?> </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) . "<br>\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"> <?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"> <?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) . "<br>\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->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: -->