diff --git a/language/types.xml b/language/types.xml index 2c165d5b32..25eeebd81c 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -12,126 +12,136 @@ Four scalar types: + - + - - - boolean - - + + + boolean + + - - - integer - - + + + integer + + - - - float (floating-point number, aka 'double') - - + + + float (floating-point number, aka 'double') + + - - - string - - + + + string + + - + + Two compound types: + - + - - - array - - + + + array + + - - - object - - + + + object + + - + + And finally two special types: + - + - - - resource - - + + + resource + + - - - NULL - - + + + NULL + + - + - This manual also introduces some - pseudo-types - for readability reasons: + + This manual also introduces some + pseudo-types for readability + reasons: + - + - - - mixed - - + + + mixed + + - - - number - - + + + number + + - - - callback - - + + + callback + + - + + + And the pseudo-variable $.... - You may also find some references to the type "double". Consider - double the same as float, the two names exist only for historic - reasons. + 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 - which that variable is used. + 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 want to check out the type and value of a certain expression, use + If you want to check out the type and value of a certain + 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 + debugging, use gettype. To check for a certain type, do + not use gettype, but use the is_type functions. Some examples: - - + + + + ]]> - - - + + - 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. + 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 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. Also, you - may be interested in viewing - the type comparison tables, - as they show examples of various type related comparisons. + situations, depending on what type it is at the time. For more information, + see the section on Type + Juggling. Also, you may be interested in viewing + the type comparison tables, as they + show examples of various type related comparisons. diff --git a/language/types/array.xml b/language/types/array.xml index 022828b74f..c1c599fe56 100644 --- a/language/types/array.xml +++ b/language/types/array.xml @@ -1,5 +1,5 @@ - + Arrays @@ -11,6 +11,7 @@ collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees. + Explanation of those data structures is beyond the scope of this manual, but you'll find at least one example for each of them. For more information we @@ -22,24 +23,24 @@ Specifying with <function>array</function> + An array can be created by the array language-construct. It takes a certain number of comma-separated key => value pairs. - - + + array( key => value , ... ) // key may be an integer or string // value may be any value - - - - - + + + + "bar", 12 => true); @@ -48,9 +49,9 @@ echo $arr["foo"]; // bar echo $arr[12]; // 1 ?> ]]> - - - + + + A key may be either an integer or a string. If a key is the standard representation of an @@ -61,10 +62,13 @@ echo $arr[12]; // 1 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" => 42)); @@ -74,16 +78,18 @@ echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?> ]]> - - - + + + If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten. - - + + + + 43, 32, 56, "b" => 12); array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?> ]]> - - - + + + As of PHP 4.3.0, the index generation behaviour described above has @@ -105,6 +111,7 @@ array(5 => 43, 6 => 32, 7 => 56, "b" => 12); the same as positive indices are. + Using &true; as a key will evaluate to integer 1 as key. Using &false; as a key will evaluate to @@ -113,33 +120,43 @@ array(5 => 43, 6 => 32, 7 => 56, "b" => 12); the empty string as key will create (or overwrite) a key with the 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 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 of brackets ("[]") to the variable name in that case. - + + + $arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value - + + + If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array. To change a certain value, just assign a new value to an element specified with its key. If you want to remove a key/value pair, you need to unset it. - - + + + + 1, 12 => 2); @@ -155,9 +172,9 @@ unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?> ]]> - - - + + + As mentioned above, if you provide the brackets with no key specified, then @@ -166,17 +183,17 @@ unset($arr); // This deletes the whole array be 0 (zero). If you specify a key that already has a value assigned to it, that value will be overwritten. - - - - As of PHP 4.3.0, the index generation behaviour described above has - changed. Now, if you append to an array in which the current maximum key - is negative, then the next key created will be zero - (0). Before, the new index would have been set to the - largest existing key + 1, the same as positive indices are. - - - + + + + As of PHP 4.3.0, the index generation behaviour described above has + changed. Now, if you append to an array in which the current maximum key + is negative, then the next key created will be zero + (0). Before, the new index would have been set to the + largest existing key + 1, the same as positive indices are. + + + Note that the maximum integer key used for this need not currently exist in the array. It simply must have existed in the @@ -238,23 +255,28 @@ Array + Useful functions + There are quite a few useful functions for working 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. If you only use "usual integer indices" (starting from zero, increasing by one), you can achieve the reindex effect by using array_values. - - + + + + 'one', 2 => 'two', 3 => 'three'); @@ -271,9 +293,8 @@ $b = array_values($a); ]]> - - + The foreach control structure exists specifically for arrays. It provides an easy way to traverse @@ -286,12 +307,15 @@ $b = array_values($a); Why is <literal>$foo[bar]</literal> wrong? + You should always use quotes around a string literal array index. For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar] wrong? You might have seen the following syntax in old scripts: - - + + + + ]]> - - + + + + This is wrong, but it works. Then, why is it wrong? The reason is that 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 @@ -311,6 +337,7 @@ echo $foo[bar]; bar, then PHP will substitute in the string 'bar' and use that. + This does not mean to always quote the key. You do not @@ -319,6 +346,7 @@ echo $foo[bar]; variables, as this will prevent PHP from interpreting them. + + More examples to demonstrate this fact: - - + + + + ]]> - - - + + + When you turn error_reporting up to show E_NOTICE level errors (such as setting it to @@ -419,26 +450,32 @@ print "Hello " . $arr['fruit']; // Hello apple error_reporting is turned down to not show them. + As stated in the syntax section, there must be an expression between the square brackets ('[' and ']'). That means that you can write things like this: - - + + + + ]]> - - + + + + This is an example of using a function return value as the array index. PHP also knows about constants, as you may have seen the E_* - ones before. + ones before. + - - + + ]]> - - + + + + Note that E_ERROR is also a valid identifier, just like bar in the first example. But the last example is in fact the same as writing: - - + + + + ]]> - - + + + + because E_ERROR equals 1, etc. + As we already explained in the above examples, $foo[bar] still works but is wrong. It works, because bar is due to @@ -472,8 +516,10 @@ $error_descriptions[8] = "This is just an informal notice"; you meant bar literally, as the string "bar", but that you forgot to write the quotes. + So why is it bad then? + At some point in the future, the PHP team might want to add another constant or keyword, or you may introduce another constant into your @@ -482,6 +528,7 @@ $error_descriptions[8] = "This is just an informal notice"; way, since they are special reserved keywords. + To reiterate, inside a double-quoted string, it's valid to @@ -491,6 +538,7 @@ $error_descriptions[8] = "This is just an informal notice"; strings. + @@ -512,8 +560,10 @@ $error_descriptions[8] = "This is just an informal notice"; the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour. - - + + + + ]]> - - + + + The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'. @@ -543,6 +594,7 @@ var_dump((array) new B()); Comparing + It is possible to compare arrays by array_diff and by Array operators. @@ -551,13 +603,14 @@ var_dump((array) new B()); Examples + The array type in PHP is very versatile, so here will be some examples to show you the full power of arrays. - - - + + + ]]> - - - - + + + Using array() @@ -657,9 +709,11 @@ Do you like yellow? Changing values of the array directly is possible since PHP 5 by passing them as reference. Prior versions need workaround: - - Collection - + + + + Collection + $color) { print_r($colors); ?> ]]> - - &example.outputs; - + + &example.outputs; + YELLOW ) ]]> - - - + + + This example creates a one-based array. - - One-based index - + + + + One-based index + 'January', 'February', 'March'); print_r($firstquarter); ?> ]]> - - &example.outputs; - + + &example.outputs; + 'March' ) ]]> - - - + + + Filling an array @@ -732,12 +788,14 @@ closedir($handle); ]]> + Arrays are ordered. You can also change the order using various 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 an array @@ -749,10 +807,12 @@ print_r($files); ]]> + Because the value of an array can be anything, it can also be another array. This way you can make recursive and multi-dimensional arrays. + Recursive and multi-dimensional arrays @@ -786,13 +846,16 @@ $juices["apple"]["green"] = "good"; ]]> + You should be aware that array assignment always involves value copying. It also means that the internal array pointer used by current and similar functions is reset. You need to use the reference operator to copy an array by reference. - - + + + + ]]> - - - + + + diff --git a/language/types/boolean.xml b/language/types/boolean.xml index 6286266fcf..7ce257a664 100644 --- a/language/types/boolean.xml +++ b/language/types/boolean.xml @@ -1,5 +1,5 @@ - + Booleans @@ -19,23 +19,27 @@ To specify a boolean literal, use either the keyword &true; or &false;. Both are case-insensitive. - - + + + + ]]> - - - + + + Usually you use some kind of operator which returns a boolean value, and then pass it on to a control structure. - - + + + + ]]> - - - + + Converting to boolean - - To explicitly convert a value to boolean, use either the - (bool) or the (boolean) cast. However, - in most cases you do not need to use the cast, since a value will be - automatically converted if an operator, function or control structure - requires a boolean argument. - - - See also Type Juggling. - + + + To explicitly convert a value to boolean, use either the + (bool) or the (boolean) cast. However, + in most cases you do not need to use the cast, since a value will be + automatically converted if an operator, function or control structure + requires a boolean argument. + + + + See also Type Juggling. + - - When converting to boolean, the following values are considered - &false;: - - - - - the boolean &false; itself - - - - - 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 member - variables (PHP 4 only) - - - - - the special type NULL - (including unset variables) - - - - - SimpleXML objects created from empty - tags - - - + + When converting to boolean, the following values are considered + &false;: + + + + + + the boolean &false; itself + + + + + 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 member + variables (PHP 4 only) + + + + + the special type NULL (including + unset variables) + + + + + SimpleXML objects created from empty + tags + + + - Every other value is considered &true; (including any - resource). - - - -1 is considered &true;, like any other non-zero - (whether negative or positive) number! - - - - + + Every other value is considered &true; (including any + resource). + + + + + -1 is considered &true;, like any other non-zero + (whether negative or positive) number! + + + + + ]]> - - - - + + + + + Floating point numbers Floating point numbers (AKA "floats", "doubles" or "real numbers") can be - specified using any of the following syntaxes: + specified using any of the following syntaxes: + - - + + ]]> - - + + + Formally: + - - + + - - + + - 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). + + 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). @@ -55,6 +59,7 @@ EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM}) 1/3 in decimal form becomes 0.3333333. . .. + So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you diff --git a/language/types/integer.xml b/language/types/integer.xml index 3f814b0edd..208142e865 100644 --- a/language/types/integer.xml +++ b/language/types/integer.xml @@ -1,10 +1,10 @@ - + Integers - An integer is a number of the set + An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}. @@ -27,10 +27,11 @@ If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x. + - - Integer literals - + + Integer literals + ]]> - - + + + Formally the possible structure for integer literals is: - - - + + + + - - + + + 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 integers. Integer size can be determined from @@ -72,18 +76,18 @@ integer : [+-]?decimal If an invalid digit is passed to octal integer (i.e. 8 or 9), the rest of the number is ignored. + - - Octal weirdness - + + Octal weirdness + ]]> - - - + + @@ -95,9 +99,10 @@ var_dump(01090); // 010 octal = 8 decimal it will be interpreted as a float instead. Also, if you perform an operation that results in a number beyond the bounds of the integer type, a float will be returned instead. + - - + + ]]> - - + + - - - Unfortunately, there was a bug in PHP so that this does not always work - correctly when there are negative numbers involved. For example: when you - do -50000 * $million, the result will be - -429496728. However, when both operands are positive - there is no problem. - - - - This is solved in PHP 4.1.0. - - - + + + Unfortunately, there was a bug in PHP so that this does not always work + correctly when there are negative numbers involved. For example: when you do + -50000 * $million, the result will be + -429496728. However, when both operands are positive + there is no problem. + + + This is solved in PHP 4.1.0. + + + There is no integer division operator in PHP. 1/2 yields the float 0.5. You can cast the value to an integer to always round it downwards, or you can use the round function. + - - + + ]]> - - - + + Converting to integer - - To explicitly convert a value to integer, use either the - (int) or the (integer) cast. However, - in most cases you do not need to use the cast, since a value will be - automatically converted if an operator, function or control structure - requires an integer argument. You can also convert a value to - integer with the function intval. - + + To explicitly convert a value to integer, use either the + (int) or the (integer) cast. However, + in most cases you do not need to use the cast, since a value will be + automatically converted if an operator, function or control structure + requires an integer argument. You can also convert a value to + integer with the function intval. + - - See also type-juggling. - + + See also type-juggling. + - - From <link linkend="language.types.boolean">booleans</link> + + From <link linkend="language.types.boolean">booleans</link> - - &false; will yield 0 (zero), and &true; will yield - 1 (one). - - + + &false; will yield 0 (zero), and &true; will yield + 1 (one). + + - - - From <link linkend="language.types.float">floating point - numbers</link> - + + + From <link linkend="language.types.float">floating point numbers</link> + - - When converting from float to integer, the number will be rounded towards zero. - + + 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! + + + - 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. - - - - Never cast an unknown fraction to integer, as this can - sometimes lead to unexpected results. - - + + ]]> - - - - See for more information the warning - about float-precision. - - - - - - From strings - - - See String conversion to - numbers - - - - - From other types - + + + - - - Behaviour of converting to integer is undefined for other types. - Currently, the behaviour is the same as if the value was first - converted to - boolean. However, do not rely on this - behaviour, as it can change without notice. - - + See for more information the warning + about float-precision. - + + + + + From strings + + + See String conversion to + numbers + + + + + From other types + + + + Behaviour of converting to integer is undefined for other types. Currently, + the behaviour is the same as if the value was first + converted to boolean. + However, do not rely on this behaviour, as it can + change without notice. + + + diff --git a/language/types/null.xml b/language/types/null.xml index 9b095801fa..b8e64286f1 100644 --- a/language/types/null.xml +++ b/language/types/null.xml @@ -1,5 +1,5 @@ - + NULL @@ -16,25 +16,25 @@ A variable is considered to be &null; if - - - - - it has been assigned the constant &null;. - - - - - it has not been set to any value yet. - - - - - it has been unset. - - - + + + + + it has been assigned the constant &null;. + + + + + it has not been set to any value yet. + + + + + it has been unset. + + + Syntax @@ -42,17 +42,17 @@ There is only one value of type &null;, and that is the case-insensitive keyword &null;. + - - + + ]]> - - - + + See also is_null and unset. diff --git a/language/types/object.xml b/language/types/object.xml index 9cf8171ceb..3794705f61 100644 --- a/language/types/object.xml +++ b/language/types/object.xml @@ -1,5 +1,5 @@ - + Objects @@ -9,9 +9,10 @@ To initialize an object, you use the new statement to instantiate the object to a variable. + - - + + do_foo(); ?> ]]> - - - + + For a full discussion, please read the section Classes and Objects. + @@ -47,18 +48,18 @@ $bar->do_foo(); properties named by array keys and with corresponding values. For any other value, a member variable named scalar will contain the value. + - - + + scalar; // outputs 'ciao' ?> ]]> - - - + + diff --git a/language/types/pseudo-types.xml b/language/types/pseudo-types.xml index 5b5b06ad13..77cb84cdfa 100644 --- a/language/types/pseudo-types.xml +++ b/language/types/pseudo-types.xml @@ -1,5 +1,5 @@ - + Pseudo-types and variables used in this documentation @@ -15,6 +15,7 @@ gettype for example will accept all PHP types, while str_replace will accept strings and arrays. + @@ -24,6 +25,7 @@ number indicates that a parameter can be either integer or float. + @@ -62,12 +64,11 @@ can be used to create an anonymous callback function. - - - - Callback function examples - - + + + Callback function examples + + ]]> - - - + + @@ -134,6 +134,7 @@ call_user_func(array('B', 'parent::who')); // A useless. void in parameters list means that the function doesn't accept any parameters. + diff --git a/language/types/resource.xml b/language/types/resource.xml index 73a7507262..b926c3ca05 100644 --- a/language/types/resource.xml +++ b/language/types/resource.xml @@ -1,5 +1,5 @@ - + Resource @@ -39,16 +39,16 @@ 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. - - - - Persistent database links are special, they are not - destroyed by the garbage collector. See also the section about - persistent - connections. - - + + + + Persistent database links are special, they are not + destroyed by the garbage collector. See also the section about + persistent + connections. + + diff --git a/language/types/string.xml b/language/types/string.xml index 8dc2db2e88..8164a9109e 100644 --- a/language/types/string.xml +++ b/language/types/string.xml @@ -1,5 +1,5 @@ - + Strings @@ -24,26 +24,26 @@ A string literal can be specified in three different ways. - - - - - single quoted - - - - - double quoted - - - - - heredoc syntax - - - + + + + single quoted + + + + + double quoted + + + + + heredoc syntax + + + + Single quoted @@ -59,25 +59,26 @@ 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. + - - - In PHP 3, a warning will be issued at the E_NOTICE - level when this happens. - - + + + In PHP 3, a warning will be issued at the E_NOTICE level + when this happens. + + - - - Unlike the two other syntaxes, - variables and escape sequences - for special characters will not be expanded when they - occur in single quoted strings. - - + + + Unlike the two other syntaxes, + variables and escape sequences + for special characters will not be expanded when they + occur in single quoted strings. + + - - + + ]]> - - - + + @@ -112,9 +112,8 @@ echo 'Variables do not $expand $either'; Double quoted - If the string is enclosed in double-quotes ("), - PHP understands more escape sequences for special - characters: + If the string is enclosed in double-quotes ("), PHP understands more escape + sequences for special characters: @@ -233,10 +232,11 @@ echo 'Variables do not $expand $either'; It is not allowed to use heredoc syntax in initializing class members. Use other string syntaxes instead. + - - Invalid example - + + Invalid example + ]]> - - - + + @@ -257,10 +256,11 @@ EOT; 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 heredoc as with strings. + - - Heredoc string quoting example - + + Heredoc string quoting example + ]]> - - - + + @@ -418,18 +417,19 @@ echo "This square is $square->{width}00 centimeters broad."; This isn't called complex because the syntax is complex, but because you can include complex expressions this way. - + - - 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 "{\$" to get a literal "{$"). Some examples to make it clear: - + + 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 "{\$" to get a literal "{$"). Some examples to make + it clear: + - - + + + @@ -516,11 +517,9 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips"; - - - - Some string examples - + + Some string examples + ]]> - - - + + @@ -553,8 +551,8 @@ $third = $str{2}; variables of other type silently returns &null;. - + @@ -720,17 +718,17 @@ $foo = "10.0 pigs " + 1.0; // $foo is float (11) 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: + - - + + \n"; ?> ]]> - - - + + Do not expect to get the code of one character by converting it to integer diff --git a/language/types/type-juggling.xml b/language/types/type-juggling.xml index d78efb34d4..317c2326de 100644 --- a/language/types/type-juggling.xml +++ b/language/types/type-juggling.xml @@ -1,5 +1,5 @@ - + Type Juggling @@ -19,9 +19,10 @@ integers, 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. + - - + + - - - + + If the last two examples above seem odd, see @@ -79,9 +79,10 @@ examples: Also, because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions: + - - + + ]]> - - - + + See the section titled String @@ -105,47 +105,47 @@ echo $a; // bar 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. + - - + + ]]> - - - + + The casts allowed are: - - - - (int), (integer) - cast to integer - - - (bool), (boolean) - cast to boolean - - - (float), (double), (real) - cast to float - - - (string) - cast to string - - - (binary) - cast to binary string (PHP 6) - - - (array) - cast to array - - - (object) - cast to object - - + + + (int), (integer) - cast to integer + + + (bool), (boolean) - cast to boolean + + + (float), (double), (real) - cast to float + + + (string) - cast to string + + + (binary) - cast to binary string (PHP 6) + + + (array) - cast to array + + + (object) - cast to object + + + (binary) casting and b prefix forward support was added in PHP 5.2.1 @@ -153,39 +153,40 @@ $bar = (boolean) $foo; // $bar is a boolean Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: + - - + + ]]> - + - - Casting a literal strings and variables to binary strings: - + + Casting a literal strings and variables to binary strings: + - + ]]> - - - + + - Instead of casting a variable to string, you can also enclose - the variable in double quotes. + Instead of casting a variable to string, you can also enclose the variable + in double quotes. + - - + + ]]> - - - + + It may not be obvious exactly what will happen when casting between certain types. For more info, see these sections: - - - - Converting to - boolean - - - Converting to - integer - - - Converting to - float - - - Converting to - string - - - Converting to - array - - - Converting to - object - - - Converting to - resource - - - - - The type comparison tables - - - + + + + + Converting to boolean + + + + + Converting to integer + + + + + Converting to float + + + + + Converting to string + + + + + Converting to array + + + + + Converting to object + + + + + Converting to + resource + + + + + + The type comparison tables + + +