note 27359 added to control-structures.switch

From: Date: Tue, 03 Dec 2002 10:55:33 +0000
Subject: note 27359 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-40293@lists.php.net to get a copy of this message
On PHP V4.2.1, running on IIS5 as a CGI, I found an anomaly with how SWITCH handled strings. I've heard that this problem doesn't exist on V4.2.3 on Apache. This snippet took the wrong branch: <?PHP $wonum = '20010E0'; SWITCH ($wonum): CASE '20010D0'; ECHO "<BR>Branching at D with wonum: " . $wonum; BREAK; CASE '20010E0'; ECHO "<BR>Branching at E with wonum: " . $wonum; BREAK; ENDSWITCH; ?> Type casting with $wonum = (STRING) '20010E0'; didn't help. Changing the order of the CASEs made no difference (it shouldn't, but...) What did work was using MD5 to force a string comparison: <?PHP $wonum = MD5('20010E0'); SWITCH ($wonum): CASE MD5('20010D0'); ECHO "<BR>Branching at D with wonum: " . $wonum; BREAK; CASE MD5('20010E0'); ECHO "<BR>Branching at E with wonum: " . $wonum; BREAK; ENDSWITCH; ?> Moral: test test test -- http://www.php.net/manual/en/control-structures.switch.php http://master.php.net/manage/user-notes.php?action=edit+27359 http://master.php.net/manage/user-notes.php?action=delete+27359 http://master.php.net/manage/user-notes.php?action=reject+27359

« previous php.notes (#40293) next »