Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > ebb1914cf182a88528b4547490db1dd8 > files > 1608

kdewebdev-quanta-doc-3.5.9-2mdv2008.1.x86_64.rpm

<HTML
><HEAD
><TITLE
>Type juggling</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Types"
HREF="language.types.html"><LINK
REL="PREVIOUS"
TITLE="Objects"
HREF="language.types.object.html"><LINK
REL="NEXT"
TITLE="Variables"
HREF="language.variables.html"></HEAD
><BODY
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.types.object.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 6. Types</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.variables.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.types.type-juggling"
>Type juggling</A
></H1
><P
>&#13;    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 <TT
CLASS="parameter"
><I
>var</I
></TT
>,
    <TT
CLASS="parameter"
><I
>var</I
></TT
> becomes a string. If you then assign an
    integer value to <TT
CLASS="parameter"
><I
>var</I
></TT
>, it becomes an
    integer.
   </P
><P
>&#13;    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.

    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$foo = "0";  // $foo is string (ASCII 48)
  3&nbsp;$foo++;      // $foo is the string "1" (ASCII 49)
  4&nbsp;$foo += 1;   // $foo is now an integer (2)
  5&nbsp;$foo = $foo + 1.3;  // $foo is now a double (3.3)
  6&nbsp;$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
  7&nbsp;$foo = 5 + "10 Small Pigs";     // $foo is integer (15)
  8&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    If the last two examples above seem odd, see <A
HREF="language.types.string.html#language.types.string.conversion"
>String
    conversion</A
>.
   </P
><P
>&#13;    If you wish to force a variable to be evaluated as a certain type,
    see the section on <A
HREF="language.types.type-juggling.html#language.types.typecasting"
>Type
    casting</A
>. If you wish to change the type of a variable, see
    <A
HREF="function.settype.html"
><B
CLASS="function"
>settype()</B
></A
>.
   </P
><P
>&#13;    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:
    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;echo "\$foo==$foo; type is " . gettype( $foo ) . "&#60;br&#62;\n";
  3&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
     The behaviour of an automatic conversion to array is currently
     undefined.
	 
     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$a = 1;       // $a is an integer
  3&nbsp;$a[0] = "f";  // $a becomes an array, with $a[0] holding "f"
  4&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     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:

     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$a = "1";     // $a is a string
  3&nbsp;$a[0] = "f";  // What about string offsets? What happens?
  4&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     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?
    </P
><P
>&#13;     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.
    </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.typecasting"
>Type casting</A
></H2
><P
>&#13;     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.

     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$foo = 10;   // $foo is an integer
  3&nbsp;$bar = (double) $foo;   // $bar is a double
  4&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></P
><P
>&#13;     The casts allowed are:
     <P
></P
><UL
><LI
><P
>(int), (integer) - cast to integer</P
></LI
><LI
><P
>(real), (double), (float) - cast to double</P
></LI
><LI
><P
>(string) - cast to string</P
></LI
><LI
><P
>(array) - cast to array</P
></LI
><LI
><P
>(object) - cast to object</P
></LI
></UL
>
    </P
><P
>&#13;     Note that tabs and spaces are allowed inside the parentheses, so
     the following are functionally equivalent:

     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$foo = (int) $bar;
  3&nbsp;$foo = ( int ) $bar;
  4&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     It may not be obvious exactly what will happen when casting
     between certain types. For instance, the following should be
     noted.
    </P
><P
>&#13;     When casting from a scalar or a string variable to an array, the
     variable will become the first element of the array:
     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$var = 'ciao';
  3&nbsp;$arr = (array) $var;
  4&nbsp;echo $arr[0];  // outputs 'ciao'  
  5&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     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':
     <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$var = 'ciao';
  3&nbsp;$obj = (object) $var;
  4&nbsp;echo $obj-&#62;scalar;  // outputs 'ciao'
  5&nbsp;      </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.object.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.variables.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Objects</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.types.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>