diff --git a/language/types.xml b/language/types.xml index 9b1916da26..b39592df2e 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -7,12 +7,11 @@ Introduction - PHP supports eight primitive - types. + PHP supports eight primitive types. - Four scalar types: + Four scalar types: @@ -103,10 +102,11 @@ - + You may also find some references to the type "double". Consider + double the same as float, the two names exist only for historic + reasons. - The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in @@ -118,13 +118,36 @@ linkend="language.expressions">expression, use var_dump. - + If you simply want a human-readable representation of the type for debugging, use gettype. To check for a certain type, do not use gettype, but use the - is_type functions. - - + is_type functions. Some + examples: + + + + + + If you would like to force a variable to be converted to a certain @@ -133,22 +156,19 @@ use the settype function on it. - Note that a variable may behave in different manners in certain + Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on Type Juggling. - - - + Booleans This is the easiest type. A boolean expresses a - truth value. It can be either &true; or - &false;. + truth value. It can be either &true; or &false;. @@ -162,11 +182,10 @@ To specify a boolean literal, use either the keyword &true; or &false;. Both are case-insensitive. - @@ -180,17 +199,18 @@ $foo = True; // assign the value TRUE to $foo \n"; } -// because you can simply type this: +// ...because you can simply type if ($show_separators) { echo "
\n"; } @@ -220,7 +240,7 @@ if ($show_separators) { the boolean - &false; + &false; itself the integer - + + + + +
- - - +
@@ -303,20 +332,23 @@ $a = 0x1A; # hexadecimal number (equivalent to 26 decimal) ]]> - +hexadecimal : 0[xX][0-9a-fA-F]+ + +octal : 0[0-7]+ + +integer : [+-]?decimal + | [+-]?hexadecimal + | [+-]?octal +]]> + + The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned @@ -372,13 +404,15 @@ var_dump($large_number); There is no integer division operator in PHP. 1/2 yields the float - 0.5. + 0.5. You can cast the value to + an integer to always round it downwards, or you can + use the round function. @@ -420,7 +454,6 @@ var_dump( 25/7 ); If the float is beyond the boundaries of integer - (usually +/- 2.15e+9 = 2^31), the result is undefined, since the float hasn't got enough precision to give an exact integer result. @@ -449,7 +482,7 @@ echo (int) ( (0.1+0.7) * 10 ); // echoes 7! From strings See String - conversion + conversion to numbers @@ -467,16 +500,7 @@ echo (int) ( (0.1+0.7) * 10 ); // echoes 7! - - @@ -485,20 +509,24 @@ echo (int) ( (0.1+0.7) * 10 ); // echoes 7! Floating point numbers (AKA "floats", "doubles" or "real numbers") can be specified using any of the following syntaxes: - + + $a = 1.234; $a = 1.2e3; $a = 7E-10; - - + + + Formally: + + +LNUM [0-9]+ +DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) +EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM}) + + The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format). + Floating point precision @@ -526,14 +554,16 @@ EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) - Converting to float For information on when and how strings are converted to floats, see the section titled String - conversion to numbers. + conversion to numbers. For values of other types, the conversion + is the same as if the value would have been converted to integer + and then to float. See the Converting + to integer section for more information. @@ -544,10 +574,8 @@ EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) A string 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. - + has no native support of Unicode. See utf8_enncode + and utf8_decode for some Unicode support. @@ -589,19 +617,19 @@ EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) enclose it in single quotes (the character '). - To specify a literal single + To specify a literal single quote, you will need to escape it with a backslash (\), 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 too will be printed! So + other character, the backslash will also be printed! So usually there is no need to escape the backslash itself. In PHP 3, a warning will be issued at the E_NOTICE level when this - happens. + happens. @@ -614,8 +642,8 @@ EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) - But the most important pre of double-quoted strings + But the most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details. @@ -706,10 +734,10 @@ echo 'I am trying to include at this point: \n a newline'; ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation. - + The closing identifier must begin in the - first column of the line. Also, the identifier used must follow + 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. @@ -776,14 +804,13 @@ EOT; Heredoc support was added in PHP 4. - + Variable parsing When a string is specified in double quotes or with - heredoc, variables are - parsed within it. + heredoc, variables are parsed within it. There are two types of syntax, a @@ -795,11 +822,10 @@ EOT; to parse a variable, an array value, or an object property. - The complex syntax was introduced in PHP 4, - - and can be recognised + The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. + Simple syntax @@ -813,7 +839,7 @@ EOT; @@ -827,19 +853,17 @@ echo "He drank some ${beer}s"; // works the one with variables. 'red' , 'banana' => 'yellow' ); +$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); -// note that this works differently outside string-quotes. +// note that this works differently outside string-quotes echo "A banana is $fruits[banana]."; echo "This square is $square->width meters broad."; @@ -860,6 +884,7 @@ echo "This square is $square->{width}00 centimeters broad."; For anything more complex, you should use the complex syntax. + Complex (curly) syntax @@ -912,8 +937,9 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips"; - For backwards compatibility, you can still use the array-braces. - However, this syntax is deprecated as of PHP 4. + For backwards compatibility, you can still use array-braces + for the same purpose. However, this syntax is deprecated as + of PHP 4.