diff --git a/language/types.xml b/language/types.xml index 6a617edb3e..c8a9b7cc2d 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,42 +1,88 @@ Types + + Introduction + + + PHP supports eight primitive + types. + + - PHP supports the following types: + Three scalar types: + + - array - - - - - booleans - - - - - floating-point numbers - + boolean + integer + - object + floating-point number (double) + string + + + + Three compound types: + + + + + + array + + + + + + object + + + + + + + + + And finally two special types: + + + + + + resource + + + + + + null + + + + + + + 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 @@ -55,11 +101,95 @@ linkend="language.types.type-juggling">Type Juggling. + + + Booleans - - This is the simpelest. Either TRUE or FALSE. - + + + This is the simpelest type. A boolean expresses a + truth value. It can be either TRUE or FALSE. + + + + You can use the special case-insensitive constants 'TRUE' and + 'FALSE' to specify a boolean value. Usually you + use some kind of operator + which returns a boolean value, and then pass it + on to a control + structure. + + + + + + + + + + + The boolean-type was introduced in PHP 4 + + + + + Converting to boolean + + See type-juggling + for general information about converting. + + + + When converting to boolean, the following values + are considered FALSE: + + + + the boolean + FALSE + + + the integer 0 (zero) + + + the float + 0.0 (zero) + + + the empty string, and the string + "0" + + + an array + with zero elements + + + an object + with zero elements + + + the special value NULL + + + + + Every other value is considered TRUE (including any + resource). + -1 is considered TRUE! + + + + + + @@ -78,23 +208,110 @@ $a = 0x12; # hexadecimal number (equivalent to 18 decimal) maximum value of about 2 billion is the usual value (that's 32 bits signed). + + + Integer overflow + + Because of the flexible type-juggling, a number + is autmatically converted to float when it is about + to overflow. + + + + + + Converting to integer + + See type-juggling for + general information about converting. + + + + From <link linkend="language.types.boolean" + >booleans</link> + + False will yield + 0 (zero), and True + will yield 1 (one). + + + + + From floating point numbers + + When converting from float to integer, the number will + be rounded towards zero. + + + + 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. + + + No warning, not even a notice will be issued in this + case! + + + + + + Never cast an unknown fraction to integer, as this can + sometimes lead to unexpected results. + +echo (int) ( (0.1+0.7) * 10 ); // echo's 7! + + + See for more information the warning + about float-precision. + + + + + From strings + + See String + conversion + + + + + + + + See also: + Arbitrary precision integeres and + Floating point numbers + + Floating point numbers - Floating point numbers ("doubles") can be specified using any of - the following syntaxes: + Floating point numbers (aka "doubles" or "real numbers") can be + specified using any of the following syntaxes: -$a = 1.234; $a = 1.2e3; +$a = 1.234; $a = 1.2e3; $a = 7E-10; + The size of a floating point number 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 It is quite usual that simple decimal fractions like 0.1 or 0.7 cannot be @@ -238,8 +455,8 @@ class foo { var $bar; function foo() { - $this->foo = 'Foo'; - $this->bar = array('Bar1', 'Bar2', 'Bar3'); + $this->foo = 'Foo'; + $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } @@ -248,7 +465,7 @@ $name = 'MyName'; echo <<<EOT My name is "$name". I am printing some $foo->foo. -Now, I am printing some {$foo->bar[1]}. +Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; ?> @@ -307,7 +524,7 @@ $last = $str[strlen($str)-1]; String parsing - echo "You should do it this way: {$arr['foo'][3]}"; - echo "You can even write {$obj->values[3]->name}"; + echo "You can even write {$obj->values[3]->name}"; echo "This is the value of the var named $name: {${$name}}"; @@ -452,14 +669,14 @@ $last = $str[strlen($str)-1]; -$foo = 1 + "10.5"; // $foo is double (11.5) -$foo = 1 + "-1.3e3"; // $foo is double (-1299) +$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 = 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) +$foo = "10.0 pigs " + 1.0; // $foo is float (11) @@ -570,7 +787,7 @@ echo "This won't work: $a[3][bar]"; $a[3]['bar'] = 'Bob'; -echo "This will work: " . $a[3][bar]; +echo "This will work: " . $a[3]['bar']; @@ -678,6 +895,66 @@ $bar->do_foo(); + + Resource + + + A resource is a special variable, holding + a reference to an external resource. Resources + are created and used by special functions. + See the appendix + for a listing of all these + functions and the corresponding resource-types. + + + + + + The resource-type was introduced in PHP 4 + + + + + Freeing resources + + + Due to the reference-counting system introduced + with PHP4's Zend-engine, it is automatically detected + when a resource is no longer referred to (just + like Java). When this is + the case, all resources that were in use for this + resource are made free by the garbage collector. + For this reason, it is rarely ever necessary to + free the memory manually by using some free_result + function. + + + Persistant database-links are special, they + are not destroyed by the + gc. See also persistent + links + + + + + + + + + + + + Null + + + + The null-type was introduced in PHP 4 + + + + + Type Juggling @@ -814,46 +1091,58 @@ $foo = ( int ) $bar; It may not be obvious exactly what will happen when casting - between certain types. For instance, the following should be - noted. + between certain types. For more info, see these sections: + + + + Converting to + boolean + + + Converting to + integer + + + + - - When converting to boolean, the following values are considered - FALSE: - - - - the boolean FALSE - - - the integer 0 (zero) - - - the float 0.0 (zero) - - - the empty string, and the string "0" - - - an array with zero elements - - - an object with zero elements - - - - Every other value is considered true. - - (-1 is considered TRUE!). - - + When casting or forcing a conversion from array to string, the result will be the word Array. When casting or forcing a conversion from object to string, the result will be - the word Object. In both cases a warning will - be issued. + the word Object. + + When casting from a scalar or a string variable to an array, the