I am trying to convert a JavaScript to php but I am not sure how to convert Math.random() and hexChars method in below JavaScript function to PHP, thanks for any help
JavaScript function:
function generateId(){
var a=[],b;
a[8]=a[13]=a[18]=a[23]="-";
a[14]="4";
for(i=0;i<36;i++)
if(!a[i]){
b=0|Math.random()*16;
a[i]=hexChars[i==19?b&3|8:b]
}
return a.join("")
}
PHP function I converted so far:
function generateId(){
$a = array();
$a[8]=$a[13]=$a[18]=$a[23]="-";
$a[14]="4";
for($i=0;$i<36;$i++){
if(!$a[$i]){
// not sure what to do here
}
}
}
Edit:
it will create a string like this: 902EA4DD-5445-4435-93F2-2A5203818255
returns a random integer from 0 to 15. The equivalent PHP code is
http://php.net/manual/en/function.rand.php
hexChars is an array. In PHP, you can use the array notation to index a character from a string: