diff --git a/language/types.xml b/language/types.xml index b39592df2e..ce01b8fac1 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -907,7 +907,7 @@ $great = 'fantastic'; echo "This is { $great}"; // won't work, outputs: This is { fantastic} echo "This is {$great}"; // works, outputs: This is fantastic echo "This square is {$square->width}00 centimeters broad."; -echo "This works: {$arr[4][3]}"; +echo "This works: {$arr[4][3]}"; // This is wrong for the same reason // as $foo[bar] is wrong outside a string. @@ -946,35 +946,15 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips"; Some string examples - Number: 9

' */ -$num = 9; -$str = "

Number: $num

"; - -/* This one will be '

Number: $num

' */ -$num = 9; -$str = '

Number: $num

'; - -/* Get the first character of a string */ +// Get the first character of a string $str = 'This is a test.'; $first = $str{0}; -/* Get the last character of a string. */ +// Get the last character of a string. $str = 'This is still a test.'; -$last = $str{strlen($str)-1}; +$last = $str{strlen($str)-1}; ?> ]]>
@@ -985,7 +965,7 @@ $last = $str{strlen($str)-1}; - Useful functions + Useful functions and operators Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this. Please @@ -1013,6 +993,7 @@ $last = $str{strlen($str)-1}; see also the character type functions. + Converting to string @@ -1145,8 +1126,7 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "
\n"; This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), - dictionary, - collection, + dictionary, collection, stack, queue and probably more. Because you can have another PHP-array as a value, you can also quite easily simulate trees. @@ -1154,10 +1134,8 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "
\n"; Explanation of those structures is beyond the scope of this manual, but you'll find at least one example for each of those structures. - For more information about those structures, we refer you to - external literature about this broad topic. - + For more information we refer you to external literature about + this broad topic. @@ -1170,43 +1148,8 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "
\n"; language-construct. It takes a certain number of comma-separated key => value - pairs. + pairs. - - A key is either an integer - or a string. - If a key is the standard representation of an - integer, it will - be interpreted as such (i.e. "8" will be interpreted - as 8, while - "08" will be interpreted as "08"). - - - A value can be anything. - - - If you omit a key, the maximum of the integer-indices is taken, and - the new key will be that maximum + 1. As integers can be negative, - this is also true for negative indices. Having e.g. the highest index - being -6 will result in being -5 - the new key. If no integer-indices exist - yet, the key will be 0 (zero). If you specify a key - that already has a value assigned to it, that value will be overwritten. - - - Using true as a key will evalute to - integer 1 as key. Using - false as a key will evalute to integer - 0 as key. Using NULL as a key - will evaluate to an empty string. Using an emptry string as key will - create (or overwrite) a key with an empty string and its value, it is - not the same as using empty brackets. - - - You cannot use arrays or objects as keys. Doing so will result in a - warning: Illegal offset type. - - array( key => key => value can be anything + + + + "bar", 12 => true); +]]> + + + + + A key is either an integer + or a string. If a key is the standard representation + of an integer, it will be interpreted as such (i.e. + "8" will be interpreted as 8, + while "08" will be interpreted as + "08"). There are no different indexed and + associative array types in PHP, there is only one array type, + which can both contain integer and string indices. + + + A value can be of any PHP type. + + + array(6 => 5, 13 => 9, "a" => 43)); +]]> + + + + + If you omit a key, the maximum of the integer-indices is taken, and + the new key will be that maximum + 1. As integers can be negative, + this is also true for negative indices. Having e.g. the highest index + being -6 will result in being -5 + the new key. If no integer-indices exist + yet, the key will be 0 (zero). If you specify a key + that already has a value assigned to it, that value will be overwritten. + + + 43, 32, 56, "b" => 12); + +// ...this array +array(5 => 43, 6 => 32, 7 => 56, "b" => 12); +]]> + + + + + Using &true; as a key will evalute to + integer 1 as key. Using + &false; as a key will evalute to integer + 0 as key. Using NULL as a key + will evaluate to an empty string. Using an emptry string as key will + create (or overwrite) a key with an empty string and its value, it is + not the same as using empty brackets. + + + You cannot use arrays or objects as keys. Doing so will result in a + warning: Illegal offset type. + Creating/modifying with square-bracket syntax You can also modify an existing array, by explicitly setting - values. + values in it. This is done by assigning values to the array while specifying the - key in brackets. You can also - omit the key, - add an empty pair + key in brackets. You can also omit the key, add an empty pair of brackets ("[]") to the variable-name in that case. $arr[key] = value; @@ -1240,41 +1243,57 @@ $arr[] = value; // value can be anything If $arr doesn't exist yet, it will be created. - So this is also - an alternative way to specify an array. + So this is also an alternative way to specify an array. To change a certain value, just assign a new value - to it. - If you want to remove a key/value pair, you need to - unset it. - + to an element specified with its key. If you want to + remove a key/value pair, you need to unset it. + + + 1, 12 => 2); + +$arr[] = 56; // This is the same as $arr[13] = 56; + // at this point of the script + +$arr["x"] = 42; // This adds a new element to + // the array with key "x" + +unset($arr[5]); // This removes the element from the array + +unset($arr); // This deletes the whole array +]]> + + - - -
Useful functions There are quite some useful function for working - with arrays, see the array-functions - section. + with arrays, see the array + functions section. The unset function allows unsetting keys of an - array. Be aware that the array will NOT be reindexed. + array. Be aware that the array will NOT be reindexed. If you only + use "usual integer indices" (starting from zero, increasing by one), + you can achive the reindex effect by using array_values. 'one', 2 => 'two', 3 => 'three' ); -unset( $a[2] ); +$a = array(1 => 'one', 2 => 'two', 3 => 'three'); +unset($a[2]); /* will produce an array that would have been defined as - $a = array( 1=>'one', 3=>'three'); + $a = array(1 => 'one', 3 => 'three'); and NOT - $a = array( 1 => 'one', 2 => 'three'); -*/ + $a = array(1 => 'one', 2 =>'three'); +*/ + +$b = array_values($a); +// Now b is array(1 => 'one', 2 =>'three') ]]> @@ -1286,8 +1305,6 @@ unset( $a[2] ); control structure exists specifically for arrays. It provides an easy way to traverse an array. - - @@ -1312,7 +1329,8 @@ echo $foo[bar]; this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes), and PHP may in future define constants which, unfortunately for your code, have the same name. It works, because the - undefined constant gets converted to a string of the same name. + undefined constant gets converted to a string of the same name + automatically for backward compatibility reasons. As stated in the This is an example of using a function return value - as the array index. PHP knows also about constants, - and you may have seen the - E_* before. + as the array index. PHP also knows about constants, + as you may have seen the E_* ones + before. @@ -1367,7 +1385,8 @@ $error_descriptions[8] = "This is just an informal notice"; So why is it bad then? At some point in the future, the PHP team might want to add another - constant or keyword, and then you get in trouble. For example, + constant or keyword, or you may introduce another constant into your + application, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special reserved keywords. @@ -1377,14 +1396,41 @@ $error_descriptions[8] = "This is just an informal notice"; When you turn error_reporting to E_ALL, you will see that PHP generates notices whenever an - index is used which is not defined - (put the line error_reporting(E_ALL); - in your script). + index is used which is not defined. + Consider this script: + + + "y"); + +// Access element with the *bad* method +echo $abc[x]; + +?> +]]> + + + The output is: + + + +Notice: Use of undefined constant x - assumed 'x' in /path/to/script.php on +line 10
+]]> +
+
- Inside a double-quoted string, an other syntax + Inside a double-quoted string, another syntax is valid. See variable parsing in strings for more details. @@ -1393,6 +1439,26 @@ $error_descriptions[8] = "This is just an informal notice";
+ + Converting to array + + + For any of the types: integer, float, string, boolean and resource, + if you convert a value to an array, you get an array with one element + (with index 0), which is the scalar value you started with. + + + + If you convert an object to an array, you get the properties (member + variables) of that object as the array's elements. The keys are the + member variable names. + + + + If you convert a &null; value to an array, you get an empty array. + + + Examples @@ -1404,24 +1470,24 @@ $error_descriptions[8] = "This is just an informal notice"; 'red' - , 'taste' => 'sweet' - , 'shape' => 'round' - , 'name' => 'apple' - , 4 // key will be 0 +$a = array( 'color' => 'red', + 'taste' => 'sweet', + 'shape' => 'round', + 'name' => 'apple', + 4 // key will be 0 ); // is completely equivalent with $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; -$a['name'] = 'apple'; +$a['name'] = 'apple'; $a[] = 4; // key will be 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; -// will result in the array array( 0 => 'a' , 1 => 'b' , 2 => 'c' ), +// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') ]]> @@ -1433,40 +1499,38 @@ $b[] = 'c'; 4 - , 'OS' => 'Linux' - , 'lang' => 'english' - , 'short_tags' => true +$map = array( 'version' => 4, + 'OS' => 'Linux', + 'lang' => 'english', + 'short_tags' => true ); // strictly numerical keys -$array = array( 7 - , 8 - , 0 - , 156 - , -10 +$array = array( 7, + 8, + 0, + 156, + -10 ); -// this is the same as array( 0 => 7, 1 => 8, ...) +// this is the same as array(0 => 7, 1 => 8, ...) -$switching = array( 10 // key = 0 - , 5 => 6 - , 3 => 7 - , 'a' => 4 - , 11 // key = 6 (maximum of integer-indices was 5) - , '8' => 2 // key = 8 (integer!) - , '02' => 77 // key = '02' - , 0 => 12 // the value 10 will be overwritten by 12 +$switching = array( 10, // key = 0 + 5 => 6, + 3 => 7, + 'a' => 4, + 11, // key = 6 (maximum of integer-indices was 5) + '8' => 2, // key = 8 (integer!) + '02' => 77, // key = '02' + 0 => 12 // the value 10 will be overwritten by 12 ); // empty array $empty = array(); ]]>
@@ -1475,9 +1539,9 @@ $empty = array(); Collection $color) { // won't work: //$color = strtoupper($color); - //works: + // works: $colors[$key] = strtoupper($color); } print_r($colors); @@ -1549,13 +1613,12 @@ Array - Filling real array + Filling an array Arrays are ordered. You can also change the order using various - sorting-functions. See array-functions - for more information. + sorting-functions. See the array + functions section for more information. You can count + the number of items in an array using the + count function. Sorting array @@ -1585,228 +1650,50 @@ print_r($files); Recursive and multi-dimensional arrays array ( "a" => "orange" - , "b" => "banana" - , "c" => "apple" - ) - , "numbers" => array ( 1 - , 2 - , 3 - , 4 - , 5 - , 6 - ) - , "holes" => array ( "first" - , 5 => "second" - , "third" +$fruits = array ( "fruits" => array ( "a" => "orange", + "b" => "banana", + "c" => "apple" + ), + "numbers" => array ( 1, + 2, + 3, + 4, + 5, + 6, + ), + "holes" => array ( "first", + 5 => "second", + "third" ) ); + +// Some examples to address values in the array above +echo $fruits["holes"][5]; // prints "second" +echo $fruits["fruits"]["a"]; // prints "orange" +unset($fruits["holes"][0]); // remove "first" + +// Create a new multi-dimensional array +$juices["apple"]["green"] = "good"; ]]> - - - - - - - @@ -2220,19 +2107,19 @@ if ($fst === $str) { Converting to integer - - Converting to - string - - - When casting from a scalar or a string variable to an array, the - variable will become the first element of the array: - - -$var = 'ciao'; -$arr = (array) $var; -echo $arr[0]; // outputs 'ciao' - - - When casting from a scalar or a string variable to an object, the