note 66222 added to language.types.string
| From: | alex dot baldacchino at email dot it | Date: | Wed, 17 May 2006 02:42:55 +0000 |
| Subject: | note 66222 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111473@lists.php.net to get a copy of this message | ||
A little note on variables types and automatic cast in context, when used in statements other than
assignment ones (that is when passed to a function, when elaborated on the right of the
"=" sign, when a "property" is recalled by the mean of an operator - such us
indexed access - and so on): I don't think that's right to say php is typeless or not
fully typed, it is yet, but types are not statically determined, instead they're dynamically
inferred during execution. This mean that each time a value is elaborated to be assigned to a
variable, the value type became the variable type, and if such operation involves an operator
allowing different types, a proper type cast occurs, but any variable type on the right side of the
assignment operator is left untuched (unless the operator implies an assignment, like
incrementing/decrementing ones).
In other words, and generally speaking about languages with dynamic, runtime inferred types, every
time I assign a value to a variable, its type changes according to the computed value, but in a cast
without self-assignment no cast should happen, instead a temporary variable should be created with
the right type and converted value assigned to that; if an operator is not defined for a type, but a
cast is possible to any allowed type, if no self assignment occurs, no type cast occurs as well,
thus the unability to access an integer by index as it whould be a string.
A possible test:
<?php
$int_var = 5;
echo "\\n int_var is of type: ".gettype($int_var);
$str_var = "12";
echo "\\n str_var is of type: ".gettype($str_var);
$temp_int = (int) $str_var;
echo "\\n temp_int is of type: ".gettype($temp_int)." and has value: $temp_int";
echo "\\n str_var after casting on the right of '=' sign is of type:
".gettype($str_var);
$addition = $int_var + $str_var;
echo "\\n addition has value: $addition; $str_var after addition is of type:
".gettype($str_var);
$str_var = $temp_int;
echo "\\n str_var after another assignment is of type: ".gettype($str_var);
?>
What does it happen?
----
Server IP: 66.163.161.117
Probable Submitter: 151.38.65.194
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- http://master.php.net/note/edit/66222
Del: integrated -- http://master.php.net/note/delete/66222/integrated
Del: useless -- http://master.php.net/note/delete/66222/useless
Del: bad code -- http://master.php.net/note/delete/66222/bad+code
Del: spam -- http://master.php.net/note/delete/66222/spam
Del: non-english -- http://master.php.net/note/delete/66222/non-english
Del: in docs -- http://master.php.net/note/delete/66222/in+docs
Del: other reasons-- http://master.php.net/note/delete/66222
Reject -- http://master.php.net/note/reject/66222
Search -- http://master.php.net/manage/user-notes.php