3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function checkCase($string) { // --- 列出标点的转换规则 $symMap = [ "." => ">", "," => "<" ]; foreach($symMap as $k => $v) $symMap[$v] = $k; // 列出标点的转换规则 --- // --- 列出各种可能情况 // $element = [["a", "A"], [".", ">"], ["b", "B"]] $element = str_split($string); foreach($element as &$item) { if(in_array($item, $symMap)) $item = [$item, $symMap[$item]]; else $item = [strtolower($item), strtoupper($item)]; } // 列出各种可能情况 --- // 记录当前的组合状态,每一项对应 $element 中的每一项 $flags = array_fill(0, count($element), 0); // 记录结果 $result = []; while(true) { $solution = ""; // 组合当前一条结果 foreach(range(0, count($element) - 1) as $i) $solution .= $element[$i][$flags[$i]]; // 改变当前状态,同时返回是否已经到达了末尾 $isEnding = function() use(&$flags, $element) { foreach(range(0, count($flags) - 1) as $i) { if($flags[$i] < count($element[$i]) - 1) { $flags[$i]++; if($i > 0) foreach(range(0, $i - 1) as $j) $flags[$j] = 0; return false; } } return true; }; if(!$isEnding()) $result[] = $solution; else break; } return $result; } var_dump(checkCase("A.B")); var_dump(checkCase("ABCD"));
Output for 5.4.0 - 7.1.0
array(7) { [0]=> string(3) "a.b" [1]=> string(3) "A.b" [2]=> string(3) "a>b" [3]=> string(3) "A>b" [4]=> string(3) "a.B" [5]=> string(3) "A.B" [6]=> string(3) "a>B" } array(15) { [0]=> string(4) "abcd" [1]=> string(4) "Abcd" [2]=> string(4) "aBcd" [3]=> string(4) "ABcd" [4]=> string(4) "abCd" [5]=> string(4) "AbCd" [6]=> string(4) "aBCd" [7]=> string(4) "ABCd" [8]=> string(4) "abcD" [9]=> string(4) "AbcD" [10]=> string(4) "aBcD" [11]=> string(4) "ABcD" [12]=> string(4) "abCD" [13]=> string(4) "AbCD" [14]=> string(4) "aBCD" }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/VOtvl on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/VOtvl on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/VOtvl on line 6
Process exited with code 255.