Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 3011

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>Strings</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Types"
HREF="language.types.html"><LINK
REL="PREVIOUS"
TITLE="Floating point numbers"
HREF="language.types.float.html"><LINK
REL="NEXT"
TITLE="Arrays"
HREF="language.types.array.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation 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.float.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Types</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.types.array.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.types.string"
></A
>Strings</H1
><P
>&#13;    A <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
> is series of characters. In PHP,
    a character is the same as a byte, that is, there are exactly
    256 different characters possible. This also implies that PHP
    has no native support of Unicode. See <A
HREF="function.utf8-encode.html"
><B
CLASS="function"
>utf8_encode()</B
></A
>
    and <A
HREF="function.utf8-decode.html"
><B
CLASS="function"
>utf8_decode()</B
></A
> for some Unicode support.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
     It is no problem for a string to become very large. 
     There is no practical bound to the size
     of strings imposed by PHP, so there is no reason at all
     to worry about long strings.
    </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.syntax"
></A
>Syntax</H2
><P
>&#13;     A string literal can be specified in three different
     ways.
     <P
></P
><UL
><LI
><P
>&#13;        <A
HREF="language.types.string.html#language.types.string.syntax.single"
>single quoted</A
>
       </P
></LI
><LI
><P
>&#13;        <A
HREF="language.types.string.html#language.types.string.syntax.double"
>double quoted</A
>
       </P
></LI
><LI
><P
>&#13;        <A
HREF="language.types.string.html#language.types.string.syntax.heredoc"
>heredoc syntax</A
>
       </P
></LI
></UL
>
    </P
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.single"
></A
>Single quoted</H3
><P
>&#13;      The easiest way to specify a simple string is to
      enclose it in single quotes (the character <TT
CLASS="literal"
>'</TT
>). 
     </P
><P
>&#13;      To specify a literal single
      quote, you will need to escape it with a backslash
      (<TT
CLASS="literal"
>\</TT
>), like in many other languages.
      If a backslash needs to occur before a single quote or at 
      the end of the string, you need to double it.
      Note that if you try to escape any
      other character, the backslash will also be printed! So
      usually there is no need to escape the backslash itself.
      <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
        In PHP 3, a warning will
        be issued at the <TT
CLASS="literal"
>E_NOTICE</TT
> level when this
        happens.
       </P
></BLOCKQUOTE
></DIV
>
      <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
        Unlike the two other syntaxes, variables will <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>not</I
></SPAN
> 
        be expanded when they occur in single quoted strings.
       </P
></BLOCKQUOTE
></DIV
>
      <DIV
CLASS="informalexample"
><A
NAME="AEN3430"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
echo 'this is a simple string';

echo 'You can also have embedded newlines in 
strings this way as it is
okay to do';

// Outputs: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.double"
></A
>Double quoted</H3
><P
>&#13;      If the string is enclosed in double-quotes ("),
      PHP understands more escape sequences for special
      characters:
     </P
><DIV
CLASS="table"
><A
NAME="AEN3435"
></A
><P
><B
>Table 7-1. Escaped characters</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>sequence</TH
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>meaning</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\n</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>linefeed (LF or 0x0A (10) in ASCII)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\r</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>carriage return (CR or 0x0D (13) in ASCII)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\t</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>horizontal tab (HT or 0x09 (9) in ASCII)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\\</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>backslash</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\$</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>dollar sign</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\"</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>double-quote</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\[0-7]{1,3}</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          the sequence of characters matching the regular
          expression is a character in octal notation
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="literal"
>\x[0-9A-Fa-f]{1,2}</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          the sequence of characters matching the regular
          expression is a character in hexadecimal notation
         </TD
></TR
></TBODY
></TABLE
></DIV
><P
>&#13;      Again, if you try to escape any other character, the
      backslash will be printed too!
     </P
><P
>&#13;      But the most important feature of double-quoted strings
      is the fact that variable names will be expanded.
      See <A
HREF="language.types.string.html#language.types.string.parsing"
>string 
      parsing</A
> for details.
     </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.heredoc"
></A
>Heredoc</H3
><P
>&#13;      Another way to delimit strings is by using heredoc syntax
      ("&#60;&#60;&#60;").  One should provide an identifier after
      <TT
CLASS="literal"
>&#60;&#60;&#60;</TT
>, then the string, and then the
      same identifier to close the quotation. 
     </P
><P
>&#13;      The closing identifier <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>must</I
></SPAN
> begin in the
      first column of the line. Also, the identifier used must follow
      the same naming rules as any other label in PHP: it must contain
      only alphanumeric characters and underscores, and must start with
      a non-digit character or underscore.
     </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;       It is very important to note that the line with the closing
       identifier contains no other characters, except 
       <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>possibly</I
></SPAN
> a semicolon (<TT
CLASS="literal"
>;</TT
>).
       That means especially that the identifier 
       <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>may not be indented</I
></SPAN
>, and there
       may not be any spaces or tabs after or before the semicolon.
       It's also important to realize that the first character before
       the closing identifier must be a newline as defined by your
       operating system. This is <TT
CLASS="literal"
>\r</TT
> on Macintosh
       for example.
      </P
><P
>&#13;       If this rule is broken and the closing identifier is not "clean"
       then it's not considered to be a closing identifier and PHP 
       will continue looking for one.  If in this case a proper closing 
       identifier is not found then a parse error will result with the 
       line number being at the end of the script.
      </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;      Heredoc text behaves just like a double-quoted string, without
      the double-quotes. This means that you do not need to escape quotes
      in your here docs, but you can still use the escape codes listed
      above. Variables are expanded, but the same care must be taken
      when expressing complex variables inside a here doc as with
      strings.
      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3492"
></A
><P
><B
>Example 7-2. Heredoc string quoting example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$str = &#60;&#60;&#60;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-&#62;foo = 'Foo';
        $this-&#62;bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo &#60;&#60;&#60;EOT
My name is "$name". I am printing some $foo-&#62;foo.
Now, I am printing some {$foo-&#62;bar[1]}.
This should print a capital 'A': \x41
EOT;
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
       Heredoc support was added in PHP 4.
      </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.parsing"
></A
>Variable parsing</H3
><P
>&#13;      When a string is specified in double quotes or with
      heredoc, <A
HREF="language.variables.html"
>variables</A
> are 
      parsed within it. 
     </P
><P
>&#13;      There are two types of syntax, a 
      <A
HREF="language.types.string.html#language.types.string.parsing.simple"
>simple</A
>
      one and a 
      <A
HREF="language.types.string.html#language.types.string.parsing.complex"
>complex</A
>
      one.
      The simple syntax is the most common and convenient, it provides a way
      to parse a variable, an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
> value, or an 
      object property.
     </P
><P
>&#13;      The complex syntax was introduced in PHP 4, and can be recognised
      by the curly braces surrounding the expression.
     </P
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="language.types.string.parsing.simple"
></A
>Simple syntax</H4
><P
>&#13;       If a dollar sign (<TT
CLASS="literal"
>$</TT
>) is encountered, the
       parser will greedily take as much tokens as possible to form a
       valid variable name. Enclose the variable name in curly
       braces if you want to explicitly specify the end of the name.
      </P
><DIV
CLASS="informalexample"
><A
NAME="AEN3511"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$beer = 'Heineken';
echo "$beer's taste is great"; // works, "'" is an invalid character for varnames
echo "He drank some $beers";   // won't work, 's' is a valid character for varnames
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;       Similarly, you can also have an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
> index or an 
       object property parsed. With array indices, the closing square 
       bracket (<TT
CLASS="literal"
>]</TT
>) marks the end of the index. For
       object properties the same rules apply as to simple variables,
       though with object properties there doesn't exist a trick like
       the one with variables.
       
       
      
      </P
><DIV
CLASS="informalexample"
><A
NAME="AEN3517"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote your array string keys 
// and do not use {braces} when outside of strings either.

// Let's show all errors
error_reporting(E_ALL);

$fruits = array('strawberry' =&#62; 'red', 'banana' =&#62; 'yellow');

// Works but note that this works differently outside string-quotes
echo "A banana is $fruits[banana].";

// Works
echo "A banana is {$fruits['banana']}.";

// Works but PHP looks for a constant named banana first
// as described below.
echo "A banana is {$fruits[banana]}.";

// Won't work, use braces.  This results in a parse error.
echo "A banana is $fruits['banana'].";

// Works
echo "A banana is " . $fruits['banana'] . ".";

// Works
echo "This square is $square-&#62;width meters broad.";

// Won't work. For a solution, see the complex syntax.
echo "This square is $square-&#62;width00 centimeters broad.";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;       For anything more complex, you should use the complex syntax.
      </P
></DIV
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="language.types.string.parsing.complex"
></A
>Complex (curly) syntax</H4
><P
>&#13;       This isn't called complex because the syntax is complex,
       but because you can include complex expressions this way.
     </P
><P
>&#13;      In fact, you can include any value that is in the namespace
      in strings with this syntax. You simply write the expression
      the same way as you would outside the string, and then include
      it in { and }. Since you can't escape '{', this syntax will
      only be recognised when the $ is immediately following the {.
      (Use "{\$" or "\{$" to get a literal "{$").
      Some examples to make it clear:
     </P
><DIV
CLASS="informalexample"
><A
NAME="AEN3524"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// Let's show all errors
error_reporting(E_ALL);

$great = 'fantastic';

// Won't work, outputs: This is { fantastic}
echo "This is { $great}";

// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";

// Works
echo "This square is {$square-&#62;width}00 centimeters broad."; 

// Works
echo "This works: {$arr[4][3]}";

// This is wrong for the same reason as $foo[bar] is wrong 
// outside a string.  In otherwords, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}"; 

// Works.  When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " . $arr['foo'][3];

echo "You can even write {$obj-&#62;values[3]-&#62;name}";

echo "This is the value of the var named $name: {${$name}}";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></DIV
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.substr"
></A
>String access by character</H3
><P
>&#13;      Characters within strings may be accessed by specifying the
      zero-based offset of the desired character after the string 
      in curly braces.
     </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
       For backwards compatibility, you can still use array-braces
       for the same purpose. However, this syntax is deprecated as
       of PHP 4.
      </P
></BLOCKQUOTE
></DIV
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3532"
></A
><P
><B
>Example 7-3. Some string examples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// Get the first character of a string
$str = 'This is a test.';
$first = $str{0};

// Get the third character of a string
$third = $str{2};

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str{strlen($str)-1}; 
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.useful-funcs"
></A
>Useful functions and operators</H2
><P
>&#13;     Strings may be concatenated using the '.' (dot) operator. Note
     that the '+' (addition) operator will not work for this. Please
     see <A
HREF="language.operators.string.html"
>String
     operators</A
> for more information.
    </P
><P
>&#13;     There are a lot of useful functions for string modification.
    </P
><P
>&#13;     See the <A
HREF="ref.strings.html"
>string functions section</A
> 
     for general functions, the regular expression functions for
     advanced find&#38;replacing (in two tastes: 
     <A
HREF="ref.pcre.html"
>Perl</A
> and 
     <A
HREF="ref.regex.html"
>POSIX extended</A
>).
    </P
><P
>&#13;     There are also <A
HREF="ref.url.html"
>functions for URL-strings</A
>,
     and functions to encrypt/decrypt strings 
     (<A
HREF="ref.mcrypt.html"
>mcrypt</A
> and 
     <A
HREF="ref.mhash.html"
>mhash</A
>).
    </P
><P
>&#13;     Finally, if you still didn't find what you're looking for,
     see also the <A
HREF="ref.ctype.html"
>character type functions</A
>.
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.casting"
></A
>Converting to string</H2
><P
>&#13;     You can convert a value to a string using the <TT
CLASS="literal"
>(string)</TT
>
     cast, or the <A
HREF="function.strval.html"
><B
CLASS="function"
>strval()</B
></A
> function. String conversion
     is automatically done in the scope of an expression for you where a
     string is needed. This happens when you use the <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
>
     or <A
HREF="function.print.html"
><B
CLASS="function"
>print()</B
></A
> functions, or when you compare a variable
     value to a string.  Reading the manual sections on <A
HREF="language.types.html"
>Types</A
> and <A
HREF="language.types.type-juggling.html"
>Type Juggling</A
> will make
     the following clearer.  See also <A
HREF="function.settype.html"
><B
CLASS="function"
>settype()</B
></A
>.
    </P
><P
>&#13;     A <A
HREF="language.types.boolean.html"
><B
CLASS="type"
>boolean</B
></A
> <TT
CLASS="constant"
><B
>TRUE</B
></TT
> value is converted to the string <TT
CLASS="literal"
>"1"</TT
>,
     the <TT
CLASS="constant"
><B
>FALSE</B
></TT
> value is represented as <TT
CLASS="literal"
>""</TT
> (empty string).
     This way you can convert back and forth between boolean and string values.
    </P
><P
> 
     An <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> or a floating point number (<A
HREF="language.types.float.html"
><B
CLASS="type"
>float</B
></A
>) 
     is converted to a string representing the number with its digits
     (including the exponent part for floating point numbers).
    </P
><P
>&#13;     Arrays are always converted to the string <TT
CLASS="literal"
>"Array"</TT
>,
     so you cannot dump out the contents of an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
> with 
     <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
> or <A
HREF="function.print.html"
><B
CLASS="function"
>print()</B
></A
> to see what is inside 
     them.  To view one element, you'd do something like <TT
CLASS="literal"
>&#13;     echo $arr['foo']</TT
>.  See below for tips on dumping/viewing the 
     entire contents.
    </P
><P
>&#13;     Objects are always converted to the string <TT
CLASS="literal"
>"Object"</TT
>.
     If you would like to print out the member variable values of an 
     <A
HREF="language.types.object.html"
><B
CLASS="type"
>object</B
></A
> for debugging reasons, read the paragraphs 
     below. If you would like to find out the class name of which an object 
     is an instance of, use <A
HREF="function.get-class.html"
><B
CLASS="function"
>get_class()</B
></A
>.
    </P
><P
>&#13;     Resources are always converted to strings with the structure
     <TT
CLASS="literal"
>"Resource id #1"</TT
> where <TT
CLASS="literal"
>1</TT
> is
     the unique number of the <A
HREF="language.types.resource.html"
><B
CLASS="type"
>resource</B
></A
> assigned by PHP during runtime.
     If you would like to get the type of the resource, use
     <A
HREF="function.get-resource-type.html"
><B
CLASS="function"
>get_resource_type()</B
></A
>.
    </P
><P
>&#13;     <TT
CLASS="constant"
><B
>NULL</B
></TT
> is always converted to an empty string.
    </P
><P
>&#13;     As you can see above, printing out the arrays, objects or resources does not
     provide you any useful information about the values themselfs. Look at the
     functions <A
HREF="function.print-r.html"
><B
CLASS="function"
>print_r()</B
></A
> and <A
HREF="function.var-dump.html"
><B
CLASS="function"
>var_dump()</B
></A
>
     for better ways to print out values for debugging.
    </P
><P
>&#13;     You can also convert PHP values to strings to store them permanently. This
     method is called serialization, and can be done with the function
     <A
HREF="function.serialize.html"
><B
CLASS="function"
>serialize()</B
></A
>. You can also serialize PHP values to
     XML structures, if you have <A
HREF="ref.wddx.html"
>WDDX</A
> support
     in your PHP setup.
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.conversion"
></A
>String conversion to numbers</H2
><P
>&#13;     When a string is evaluated as a numeric value, the resulting
     value and type are determined as follows.
    </P
><P
>&#13;     The string will evaluate as a <A
HREF="language.types.float.html"
><B
CLASS="type"
>float</B
></A
> if it contains any of the
     characters '.', 'e', or 'E'. Otherwise, it will evaluate as an
     integer.
    </P
><P
>&#13;     The value is given by the initial portion of the string. If the
     string starts with valid numeric data, this will be the value
     used. Otherwise, the value will be 0 (zero). Valid numeric data
     is an optional sign, followed by one or more digits (optionally
     containing a decimal point), followed by an optional
     exponent. The exponent is an 'e' or 'E' followed by one or more
     digits.
    </P
><DIV
CLASS="informalexample"
><A
NAME="AEN3598"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-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 = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)     
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;     For more information on this conversion, see the Unix manual page
     for strtod(3).
    </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"
><A
NAME="AEN3602"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
echo "\$foo==$foo; type is " . gettype ($foo) . "&#60;br /&#62;\n";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     Do not expect to get the code of one character by converting it
     to integer (as you would do in C for example). Use the functions
     <A
HREF="function.ord.html"
><B
CLASS="function"
>ord()</B
></A
> and <A
HREF="function.chr.html"
><B
CLASS="function"
>chr()</B
></A
> to convert
     between charcodes and characters.
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.float.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.types.array.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Floating point numbers</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.types.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Arrays</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>