note 66222 deleted from language.types.string by colder

From: Date: Tue, 17 Oct 2006 18:32:09 +0000
Subject: note 66222 deleted from language.types.string by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-118652@lists.php.net to get a copy of this message
Note Submitter: alex dot baldacchino at email dot it ---- 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?

« previous php.notes (#118652) next »