Welcome Guest, Not a member yet? Register   Sign In
Method always returns the same value (cache?)
#1

(This post was last modified: 03-23-2022, 02:56 PM by RedskyThirty.)

Hello,

I hadĀ a big mistake today that I was not able to solve.
I have a method to generate a random string:

PHP Code:
function RS_RANDOM_STRING(int $lengthbool $onlyLetters falsebool $onlyNumbers false): string
 
{
 
$chars '';
 if (!
$onlyNumbers || $onlyLetters$chars .= 'abcdefghijklmnopqrstuvwxyz';
 if (!
$onlyLetters || $onlyNumbers$chars .= '0123456789';
 
 
$numChars strlen($chars);
 
$loop ceil($length $numChars);
 
 return 
substr(str_shuffle(str_repeat($chars$loop)), 0$length);
 } 

Sometimes, when this method is called, it always returns the same value as if it has beenĀ cached.
Another strange thing is that if I call the following method instead of the previous one, I get a new value at each call so no more "cache" mistake.

PHP Code:
function RS_GENERATE_GUID(bool $lowercase truebool $separated false): string
 
{
 
mt_srand((int)microtime()*10000);
 
$charid md5(uniqid(rand(), true));
 
$hyphen $separated chr(45) : ''// chr(45) = "-"
 
$guid substr($charid08).$hyphen
 
.substr($charid84).$hyphen
 
.substr($charid,124).$hyphen
 
.substr($charid,164).$hyphen
 
.substr($charid,20,12);
 
 return 
$lowercase $guid strtoupper($guid);
 } 

I'm pretty sure I didn't have this problem with older versions of the framework and when I was in PHP 7.4 (now 8.1.2).
Does anyone understand why the first method seems to be cached sometimes and always return the same value and the second one always return a new value?
Reply


Messages In This Thread
Method always returns the same value (cache?) - by RedskyThirty - 03-23-2022, 01:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB