Re: [PHP] Re: Watch out for automatic type casting
On Sat, Apr 7, 2012 at 1:55 PM, Maciek Sokolewicz maciek.sokolew...@gmail.com wrote: On 07-04-2012 16:37, Bogdan Ribic wrote: [snip] Bogdan, you are reviving a thread over a week old, and repeating what 4 other people have already stated. Please don't do that, it just results in mailinglist-noise. - Tul plus, you top-posted /piling-on -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Watch out for automatic type casting
This is *not* typecasting at all, this is assignment of a result of boolean operator, and it boils down to operator precedence. It's equivalent to this code: $b = $x == 11; in the part that right side of equation sign is calculated first, then assigned to lvalue. In effect, you wrote this: $pos = (strpos($sText, test) !== FALSE); You would often use something like this in code: $isSame = $x == $y; which would put a boolean into $isSame, not put value of $x and then compare it. On 3/29/2012 18:57, Arno Kuhl wrote: I found automatic typecasting can be a bit of a gotcha. $sText = this.is.a.test.text; if ( $pos = strpos($sText, test) !== FALSE) { echo substr($sText, 0, $pos)..substr($sText, $pos, strlen(test))..substr($sText, $pos+strlen(test)); } The code seems logical enough, and the expected result would be: this.is.a.test.text In fact it ends up being: this.is.a.test.text The reason is $pos is typecast as TRUE, not int 10, presumably because it's in the same scope as the boolean test. -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Watch out for automatic type casting
On 07-04-2012 16:37, Bogdan Ribic wrote: This is *not* typecasting at all, this is assignment of a result of boolean operator, and it boils down to operator precedence. It's equivalent to this code: $b = $x == 11; in the part that right side of equation sign is calculated first, then assigned to lvalue. In effect, you wrote this: $pos = (strpos($sText, test) !== FALSE); You would often use something like this in code: $isSame = $x == $y; which would put a boolean into $isSame, not put value of $x and then compare it. Bogdan, you are reviving a thread over a week old, and repeating what 4 other people have already stated. Please don't do that, it just results in mailinglist-noise. - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php