diff --git a/language/types.xml b/language/types.xml index 53324d26a4..2e0cc480d0 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,9 +1,8 @@ - - Types - - - PHP supports the following types: + + Types + + PHP supports the following types: @@ -12,7 +11,8 @@ - floating-point numbers + floating-point numbers + @@ -32,20 +32,17 @@ - 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 which that variable is used. - If you would like to force a variable to be converted to a certain type, you may either cast the variable or use the settype function on it. - Note that a variable may behave in different manners in certain situations, depending on what type it is at the time. For more @@ -146,7 +143,6 @@ $a = 1.234; $a = 1.2e3; You can escape any other character, but a warning will be issued at the highest warning level. - The second way to delimit a string uses the single-quote ("'") character. When a string is enclosed in single quotes, the only @@ -155,7 +151,6 @@ $a = 1.234; $a = 1.2e3; a single-quoted string. Variables will not be expanded inside a single-quoted string. - Another way to delimit strings is by using here doc syntax ("<<<"). One should provide an identifier after @@ -178,20 +173,17 @@ EOD; Here doc support was added in PHP 4. - Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this. Please see String operators for more information. - Characters within strings may be accessed by treating the string as a numerically-indexed array of characters, using C-like syntax. See below for examples. - Some string examples @@ -231,13 +223,13 @@ $last = $str[strlen($str)-1]; When a string is evaluated as a numeric value, the resulting - value and type are determined as follows. - + value and type are determined as follows. + The string will evaluate as a double if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will evaluate as an - integer. - + integer. + The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value @@ -245,13 +237,12 @@ $last = $str[strlen($str)-1]; 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. - + digits. + When the first expression is a string, the type of the variable will depend on the second expression. - $foo = 1 + "10.5"; // $foo is double (11.5) @@ -264,19 +255,17 @@ $foo = "10.0 pigs " + 1; // $foo is integer (11) $foo = "10.0 pigs " + 1.0; // $foo is double (11) - For more information on this conversion, see the Unix manual page for strtod(3). - 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: -echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n"; +echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n"; @@ -289,7 +278,8 @@ echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n"; Arrays actually act like both hash tables (associative arrays) and - indexed arrays (vectors). + indexed arrays (vectors). + Single Dimension Arrays @@ -299,7 +289,6 @@ echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n"; is no difference between the two. You can create an array using the list or array functions, or you can explicitly set each array element value. - $a[0] = "abc"; @@ -308,36 +297,34 @@ $b["foo"] = 13; - You can also create an array by simply adding values to the array. When you assign a value to an array variable using empty brackets, the value will be added onto the end of the array. - $a[] = "hello"; // $a[2] == "hello" $a[] = "world"; // $a[3] == "world" - - + + Arrays may be sorted using the asort, arsort, ksort, rsort, sort, uasort, usort, and uksort functions depending on the type of - sort you want. - + sort you want. + You can count the number of items in an array using the - count function. - + count function. + - You can traverse an array using next and - prev functions. Another common way to - traverse an array is to use the each - function. + You can traverse an array using next and + prev functions. Another common way to + traverse an array is to use the each + function. @@ -347,7 +334,6 @@ $a[] = "world"; // $a[3] == "world" Multi-dimensional arrays are actually pretty simple. For each dimension of the array, you add another [key] value to the end: - $a[1] = $f; # one dimensional examples @@ -359,8 +345,8 @@ $a[3]["bar"] = $f; # (you can mix numeric and associative indices) $a["foo"][4]["bar"][0] = $f; # four dimensional! - - + + In PHP3 it is not possible to reference multidimensional arrays directly within strings. For instance, the following will not @@ -371,25 +357,20 @@ $a[3]['bar'] = 'Bob'; echo "This won't work: $a[3][bar]"; - In PHP3, the above will output This won't work: Array[bar]. The string concatenation operator, however, can be used to overcome this: - $a[3]['bar'] = 'Bob'; echo "This will work: " . $a[3][bar]; - - In PHP4, however, the whole problem may be circumvented by enclosing the array reference (inside the string) in curly braces: - $a[3]['bar'] = 'Bob'; @@ -397,14 +378,12 @@ echo "This will work: {$a[3][bar]}"; - You can "fill up" multi-dimensional arrays in many ways, but the trickiest one to understand is how to use the array command for associative arrays. These two snippets of code fill up the one-dimensional array in the same way: - # Example 1: @@ -424,12 +403,11 @@ $a = array( 3 => 4 ); - - + + The array function can be nested for multi-dimensional arrays: - <? @@ -471,10 +449,10 @@ echo $a["apple"]["taste"]; # will output "sweet" statement to instantiate the object to a variable. - + <?php class foo { - function do_foo () { + function do_foo() { echo "Doing foo."; } } @@ -485,17 +463,16 @@ $bar->do_foo(); - For a full discussion, please read the section Classes and Objects. + linkend="language.oop">Classes and Objects. - Type juggling + Type Juggling PHP does not require (or support) explicit type definition in @@ -506,7 +483,6 @@ $bar->do_foo(); integer value to var, it becomes an integer. - An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a double, then all @@ -515,7 +491,6 @@ $bar->do_foo(); 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. - $foo = "0"; // $foo is string (ASCII 48) @@ -527,37 +502,31 @@ $foo = 5 + "10 Small Pigs"; // $foo is integer (15) - If the last two examples above seem odd, see String conversion. - If you wish to force a variable to be evaluated as a certain type, see the section on Type casting. If you wish to change the type of a variable, see settype. - 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: -echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n"; +echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n"; - - The behaviour of an automatic conversion to array is currently undefined. - $a = 1; // $a is an integer @@ -565,12 +534,10 @@ $a[0] = "f"; // $a becomes an array, with $a[0] holding "f" - 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: - $a = "1"; // $a is a string @@ -578,14 +545,12 @@ $a[0] = "f"; // What about string offsets? What happens? - 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? - 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 @@ -594,20 +559,19 @@ $a[0] = "f"; // What about string offsets? What happens? - Type casting + Type Casting 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. - $foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double - - + + The casts allowed are: @@ -628,11 +592,9 @@ $bar = (double) $foo; // $bar is a double - Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: - $foo = (int) $bar; @@ -640,13 +602,11 @@ $foo = ( int ) $bar; - It may not be obvious exactly what will happen when casting between certain types. For instance, the following should be noted. - When casting from a scalar or a string variable to an array, the variable will become the first element of the array: @@ -658,7 +618,6 @@ echo $arr[0]; // outputs 'ciao' - When casting from a scalar or a string variable to an object, the variable will become an attribute of the object; the attribute @@ -677,19 +636,19 @@ echo $obj->scalar; // outputs 'ciao' - +