3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* PARAMETERS: * $length(int) = length of final code (excluding separators) * $type(string) = type of code (numbers|letters|mixed)("mixed" by default) * $size(string) = size of code (uppercase|lowercase|mixed)("uppercase" by default) * $sep(array) = * [0](bool) : if use separator ("false" by default) * [1](int) : frequency of separation ("5" by defaul$next != null && t) * [2](string) : separator ("-" by default) * RETURNS: * $code(string) = random-generated code */ function generateCode($length, $type = "mixed", $size = "uppercase", $sep = array(false, 5, "-")){ $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $nums = "0123456789"; if($type == "numbers") $arr = $nums; else if($type == "letters") $arr = $chars; else if($type == "mixed") $arr = $nums.$chars; echo $arr."<br>"; // HERE I GET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" if($size == "mixed") $arr .= strtolower($chars); // HERE IS THE PROBLEM else if($size == "lowercase"); $arr = strtolower($arr); echo $arr."<br>"; // HERE I GET "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" // BUT I SHOULD GET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" $arr = str_split($arr); if($sep[0] == true){ $length += ceil($length / $sep[1] - 1); $next = $sep[1]; } for($i = 0; $i < $length; $i++){ if($next != null && $next == $i){ $next += $sep[1] + 1; $code .= $sep[2]; } else $code .= $arr[rand(0, sizeof($arr) - 1)]; } return $code; } echo generateCode(5, "mixed", "mixed");

preferences:
34.12 ms | 402 KiB | 5 Q