PHP: Hypertext Preprocessor (original) (raw)
Type Juggling
PHP does not require explicit type definition in variable declaration. In this case, the type of a variable is determined by the value it stores. That is to say, if a string is assigned to variable$var, then varisoftype[string](language.types.string.php).Ifafterwardsan[int](language.types.integer.php)valueisassignedtovar is of typestring. If afterwards an int value is assigned to varisoftype[string](language.types.string.php).Ifafterwardsan[int](language.types.integer.php)valueisassignedtovar, it will be of type int.
PHP may attempt to convert the type of a value to another automatically in certain contexts. The different contexts which exist are:
- Numeric
- String
- Logical
- Integral and string
- Comparative
- Function
Note: When a value needs to be interpreted as a different type, the value itself does not change types.
To force a variable to be evaluated as a certain type, see the section onType casting. To change the type of a variable, see the settype() function.
Numeric contexts
This is the context when using anarithmetical operator.
In this context if either operand is a float (or not interpretable as an int), both operands are interpreted asfloats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted aTypeError is thrown.
Integral and string contexts
This is the context when usingbitwise operators.
In this context if all operands are of type string the result will also be a string. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted aTypeError is thrown.
Comparative contexts
This is the context when using acomparison operator.
The type conversions which occur in this context are explained in the Comparison with Various Typestable.
Function contexts
This is the context when a value is passed to a typed parameter, property, or returned from a function which declares a return type.
In this context the value must be a value of the type. Two exceptions exist, the first one is: if the value is of typeint and the declared type is float, then the integer is converted to a floating point number. The second one is: if the declared type is a scalar type, the value is convertable to a scalar type, and the coercive typing mode is active (the default), the value may be converted to an accepted scalar value. See below for a description of this behaviour.
Warning
Internal functions automatically coerce [null](reserved.constants.php#constant.null)
to scalar types, this behaviour is DEPRECATED as of PHP 8.1.0.
Coercive typing with simple type declarations
- bool type declaration: value is interpreted as bool.
- int type declaration: value is interpreted as int if the conversion is well-defined. For example the string isnumeric.
- float type declaration: value is interpreted as float if the conversion is well-defined. For example the string isnumeric.
- string type declaration: value is interpreted as string.
Coercive typing with union types
When strict_types
is not enabled, scalar type declarations are subject to limited implicit type coercions. If the exact type of the value is not part of the union, then the target type is chosen in the following order of preference:
If the type exists in the union and the value can be coerced to the type under PHP's existing type-checking semantics, then the type is chosen. Otherwise, the next type is tried.
Caution
As an exception, if the value is a string and both int and float are part of the union, the preferred type is determined by the existingnumeric string semantics. For example, for "42"
int is chosen, while for "42.0"
float is chosen.
Note:
Types that are not part of the above preference list are not eligible targets for implicit coercion. In particular no implicit coercions to the null, false, and true types occur.
Example #1 Example of types being coerced into a type part of the union
42 // exact type "42" --> "42" // exact type new ObjectWithToString --> "Result of __toString()" // object never compatible with int, fall back to string 42.0 --> 42 // float compatible with int 42.1 --> 42 // float compatible with int 1e100 --> "1.0E+100" // float too large for int type, fall back to string INF --> "INF" // float too large for int type, fall back to string true --> 1 // bool compatible with int [] --> TypeError // array not compatible with int or string // int|float|bool "45" --> 45 // int numeric string "45.0" --> 45.0 // float numeric string "45X" --> true // not numeric string, fall back to bool "" --> false // not numeric string, fall back to bool "X" --> true // not numeric string, fall back to bool [] --> TypeError // array not compatible with int, float or bool ?>Type Casting
Type casting converts the value to a chosen type by writing the type within parentheses before the value to convert.
Example #2 Type Casting
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi><mo>=</mo><mn>10</mn><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi></mrow><annotation encoding="application/x-tex">foo = 10; // </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">10</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span></span></span></span>foo is an integer <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>a</mi><mi>r</mi><mo>=</mo><mo stretchy="false">(</mo><mi>b</mi><mi>o</mi><mi>o</mi><mi>l</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">bar = (bool) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">ba</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">b</span><span class="mord mathnormal">oo</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mclose">)</span></span></span></span>foo; // <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>s</mi><mi>a</mi><mi>b</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>e</mi><mi>a</mi><mi>n</mi><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">bar is a booleanvar_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">ba</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">oo</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">an</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>bar); ?>
The casts allowed are:
(int)
- cast to int(bool)
- cast to bool(float)
- cast to float(string)
- cast to string(array)
- cast to array(object)
- cast to object(unset)
- cast to NULL
Note:
(integer)
is an alias of the(int)
cast.(boolean)
is an alias of the(bool)
cast.(binary)
is an alias of the(string)
cast.(double)
and(real)
are aliases of the(float)
cast. These casts do not use the canonical type name and are not recommended.
Warning
The (real)
cast alias has been deprecated as of PHP 8.0.0.
Warning
The (unset)
cast has been deprecated as of PHP 7.2.0. Note that the (unset)
cast is the same as assigning the value NULL to the variable or call. The (unset)
cast is removed as of PHP 8.0.0.
Caution
The (binary)
cast and b
prefix exists for forward support. Currently (binary)
and(string)
are identical, however this may change and should not be relied upon.
Note:
Whitespaces are ignored within the parentheses of a cast. Therefore, the following two casts are equivalent:
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">foo = (int) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mclose">)</span></span></span></span>bar; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">foo = ( int ) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mclose">)</span></span></span></span>bar; ?>
Casting literal strings and variables to binarystrings:
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>r</mi><mi>y</mi><mo>=</mo><mo stretchy="false">(</mo><mi>b</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>r</mi><mi>y</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">binary = (binary) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">bina</span><span class="mord mathnormal" style="margin-right:0.03588em;">ry</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">bina</span><span class="mord mathnormal" style="margin-right:0.03588em;">ry</span><span class="mclose">)</span></span></span></span>string; $binary = b"binary string"; ?>
Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes.
Example #3 Different Casting Mechanisms
`<?php foo=10;//foo = 10; // foo=10;//foo is an integer str="str = "str="foo"; // strisastring<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>s</mi><mi>t</mi><mo>=</mo><mostretchy="false">(</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x−tex">fst=(string)</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mopen">(</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">in</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mclose">)</span></span></span></span>foo;//str is a string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>s</mi><mi>t</mi><mo>=</mo><mo stretchy="false">(</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">fst = (string) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mclose">)</span></span></span></span>foo; // strisastring<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>s</mi><mi>t</mi><mo>=</mo><mostretchy="false">(</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x−tex">fst=(string)</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mopen">(</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">in</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mclose">)</span></span></span></span>foo;//fst is also a string
// This prints out that "they are the same"
if ($fst === $str) {
echo "they are the same", PHP_EOL;
}
?>`
It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
- The type comparison tables
Note: Because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions:
Example #4 Using Array Offset with a String
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><msup><mo>=</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>c</mi><mi>a</mi><msup><mi>r</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi></mrow><annotation encoding="application/x-tex">a = 'car'; // </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7519em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><span class="mrel">=</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span></span></span></span>a is a string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mo stretchy="false">[</mo><mn>0</mn><mo stretchy="false">]</mo><msup><mo>=</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mi>b</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi></mrow><annotation encoding="application/x-tex">a[0] = 'b'; // </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mopen">[</span><span class="mord">0</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><span class="mrel">=</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span></span></span></span>a is still a string echo $a; // bar ?>
See the section titled String access by character for more information.
Found A Problem?
20 years ago
`Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example):
$dividend = 2; divisor=3;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>q</mi><mi>u</mi><mi>o</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">quotient=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.854em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.03588em;">q</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>dividend/divisor = 3; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>q</mi><mi>u</mi><mi>o</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">quotient = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="mord mathnormal">u</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>dividend/divisor=3;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>q</mi><mi>u</mi><mi>o</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">quotient=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.854em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.03588em;">q</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>dividend/divisor;
print $quotient; // 0.66666666666667
`
4 years ago
Cast operators have a very high precedence, for example (int)$a/$b is evaluated as ((int)$a)/$b, not as (int)($a/$b) [which would be like intdiv($a,$b) if both <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>a</mi><mi>n</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">a and </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">aan</span><span class="mord mathnormal">d</span></span></span></span>b are integers]. The only exceptions (as of PHP 8.0) are the exponentiation operator ** [i.e. (int)$a**$b is evaluated as (int)($a**$b) rather than ((int)$a)**$b] and the special access/invocation operators ->, ::, [] and () [i.e. in each of (int)$a->$b, (int)$a::$b, (int)$a[$b] and (int)$a($b), the cast is performed last on the result of the variable expression].
12 years ago
`Casting objects to arrays is a pain. Example:
'; print_r((array) $test);/* Array ( [MyClasspriv] => priv_value [*prot] => prot_value [pub] => pub_value [MyClasspriv] => second_pub_value ) */?>Yes, that looks like an array with two keys with the same name and it looks like the protected field was prepended with an asterisk. But that's not true:
testastest as testaskey => $value) { len=strlen(len = strlen(len=strlen(key); echo "{$key} ({$len}) => {$value}"; for ($i = 0; i<i < i<len; ++$i) { echo ord($key[$i]) . ' '; } echo '
'; }/* MyClasspriv (13) => priv_value 0 77 121 67 108 97 115 115 0 112 114 105 118 *prot (7) => prot_value 0 42 0 112 114 111 116 pub (3) => pub_value 112 117 98 MyClasspriv (11) => second_pub_value 77 121 67 108 97 115 115 112 114 105 118 */?>
The char codes show that the protected keys are prepended with '\0*\0' and private keys are prepended with '\0'.CLASS.'\0' so be careful when playing around with this.
`
miracle at 1oo-percent dot de ¶
19 years ago
`If you want to convert a string automatically to float or integer (e.g. "0.234" to float and "123" to int), simply add 0 to the string - PHP will do the rest.
e.g.
$val = 0 + "1.234";
(type of $val is float now)
$val = 0 + "123";
(type of $val is integer now)
`
14 years ago
`The object casting methods presented here do not take into account the class hierarchy of the class you're trying to cast your object into.
/**
- Convert an object to a specific class.
- @param object $object
- @param string $class_name The class to cast the object to
- @return object
/
public static function cast($object, $class_name) {
if($object === false) return false;
if(class_exists($class_name)) {
serobject=serialize(ser_object = serialize(serobject=serialize(object);
objnamelen=strlen(getclass(obj_name_len = strlen(get_class(objnamelen=strlen(getclass(object));
start=start = start=obj_name_len + strlen($obj_name_len) + 6;
newobject=′O:′.strlen(new_object = 'O:' . strlen(newobject=′O:′.strlen(class_name) . ':"' . $class_name . '":';
newobject.=substr(new_object .= substr(newobject.=substr(ser_object, $start);
newobject=unserialize(new_object = unserialize(newobject=unserialize(new_object);
/* - The new object is of the correct type but
- is not fully initialized throughout its graph.
- To get the full object graph (including parent
- class data, we need to create a new instance of
- the specified class and then assign the new
- properties to it.
*/
graph=newgraph = new graph=newclass_name;
foreach($new_object as prop=>prop => prop=>val) {
graph−>graph->graph−>prop = $val;
}
return $graph;
} else {
throw new CoreException(false, "could not find class $class_name for casting in DB::cast");
return false;
}
}
`
22 years ago
Printing or echoing a FALSE boolean value or a NULL value results in an empty string: (string)TRUE //returns "1" (string)FALSE //returns "" echo TRUE; //prints "1" echo FALSE; //prints nothing!
ieee at REMOVE dot bk dot ru ¶
12 years ago
There are some shorter and faster (at least on my machine) ways to perform a type cast. <?php $string='12345.678'; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>l</mi><mi>o</mi><mi>a</mi><mi>t</mi><mo>=</mo><mo>+</mo></mrow><annotation encoding="application/x-tex">float=+</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">o</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">+</span></span></span></span>string; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo>=</mo><mn>0</mn><mi mathvariant="normal">∣</mi></mrow><annotation encoding="application/x-tex">integer=0|</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0∣</span></span></span></span>string; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>e</mi><mi>a</mi><mi>n</mi><mo>=</mo><mo stretchy="false">!</mo><mo stretchy="false">!</mo></mrow><annotation encoding="application/x-tex">boolean=!!</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">oo</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">an</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mclose">!!</span></span></span></span>string; ?>
6 months ago
`Type casting in expressions is executed first.
The casting is assigned to the value, not to the expression result.
Examples:
`
bioingbenito at gmail dot com ¶
29 days ago
This PHP documentation is very thorough. An observation about comments, maybe they lost sense when become old, and the page changes, so, should be signaled this condition, or add a link to the document version when the comment was made.