Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:15:41+00:00 2026-06-14T14:15:41+00:00

I am trying to convert a JavaScript to php but I am not sure

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:15:42+00:00Added an answer on June 14, 2026 at 2:15 pm
    b = 0|Math.random()*16;
    

    returns a random integer from 0 to 15. The equivalent PHP code is

    $b = rand(0,15);
    

    http://php.net/manual/en/function.rand.php


    a[i]=hexChars[i==19?b&3|8:b]
    

    hexChars is an array. In PHP, you can use the array notation to index a character from a string:

    $hexChars = "0123456789ABCDEF"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert the following jquery to javascript syntax, but being not familiar
Im trying to convert this php function to javascript: function sanitize_words($string,$limit=false) { preg_match_all(/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]{1,}/u,$string,$matches,PREG_PATTERN_ORDER); return
Am trying to pass an array of values from PHP function to Javascript. Not
I was trying to convert basic Javascript function into PHP, and I saw that
I am new to PHP and I'm trying to convert a simple Javascript function
I'm trying to convert a function from Javascript to CoffeeScript. This is the code:
i m trying to convert php mysql_fetch_row result into javascript array so i write
I am trying to convert a JavaScript hashing function to C# hashing to do
I'm pretty familiar with PHP, Javascript, etc but I'm new to Powershell. I'm trying
Ok, Im trying to convert a javascript timestamp to a php equivalent, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.