|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-12-27 20:39 UTC] rasmus@php.net
-Status: Open
+Status: Duplicate
[2014-01-01 09:04 UTC] hamidreza dot mz712 at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 14:00:01 2025 UTC |
Description: ------------ As mentioned in docs, switch statement is a loose type comparison tool. But this may be lead to a serious bug in user products. It would be really better to force it to use strong type comparison (or have an option such as an optional 2nd argument let the users to do so). In the real world, we have no other loose type languages that behaves like this. For example, in JavaScript, the result is fine. I mean, although being a loose type language is a good feature for PHP in overall; It should not be led to unexpected results those not shown in any other language (even the other loose type ones). I really now the process flow. It uses if, elseif, else statements like this: if($v == 'hello') { echo 'Hello'; } elseif($v == 'goodbye') { echo 'Goodbye'; } else { echo 'Error'; } And 'hello' is converted to integer (with intval('hello') or anything similar) and because it does not contain an integer value at the beginning, it uses the default integer value (zero) and so, the first case becomes true. All I want to say is that this behavior is incorrect because approximately always it's not the behavior that the developer expected. Test script: --------------- $v = 0; switch($v) { case 'hello': echo 'Hello'; break; case 'goodbye': echo 'Goodbye'; break; default: echo 'Error'; break; } Expected result: ---------------- Error Actual result: -------------- Hello