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 8168849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:39:43+00:00 2026-06-06T20:39:43+00:00

i have a function function createRandomString($length) { $string = md5(time()); $highest_startpoint = 32-$length; #

  • 0

i have a function

function createRandomString($length) {
    $string = md5(time());
    $highest_startpoint = 32-$length;
    # use hexdec to get a "number only" format
    $randomString = hexdec(substr($string,rand(0,$highest_startpoint),$length));
    return $randomString;
}
$randomID = createRandomString(7);

and i want to be able to use the random string created in other functions

for example:

function new() {
echo '<input name="id" type="text" value="'.$randomID.'" disabled="disabled">';
}

However the random ID does not show up in the new function (aka its blank).

I need to define the random ID as a variable once so that the number remains constant throughout the script since if I defined it in every function you would get a different number using this method.

Is there a way of defining the variable randomID using the script at the top and keeping it constant throughout all functions calling it?

  • 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-06T20:39:44+00:00Added an answer on June 6, 2026 at 8:39 pm

    Well, though I’d advise against it, the easiest fix for what you’re trying to do in the new function is:

    function new()
    {
        global $randomID;
        echo '<input....'.$randomID.'/>';
    }
    

    But there are a few more, and better ways to achieve the same result:

    using classes:

    class randomOutput
    {
        protected static $_randomID;
        public function __construct()
        {
            self::$_randomID;//can be initialized here, using the same code you have in your createRandomId function
        }
    
       public function output()
       {
           echo '<input...'.self::$_randomID.'/>';
       }
    }
    

    using arguments, as Zulkhaery Basrul’s answer shows. You can make the argument optional by giving a default value of null in your function defenition.
    However, since that answer is now deleted:

    function new($id)
    {
        echo '<input...'.$id.'/>';
    }
    
    //with default val:
    function new($id = null)
    {
        $id = ($id === null ? createRandomId(7) : $id);
        echo '<input...'.$id.'/>';
    }
    
    $randId = createRandomId(7);
    new($randId);
    //some time later
    new($randId);
    

    Declaring constants:

    function createRandomString($length)
    {
        if (defined(RANDOM_STRING_CONST))
        {
            return RANDOM_STRING_CONST;
        }
        //make your randomID, then:
        define('RANDOM_STRING_CONST',$randomID);
        return RANDOM_STRING_CONST;
    }
    

    Classes have constants, too. You can define them in an abstract class, so all child classes have access to the same randomId. As an added bonus (and potential pitfall) class constants, defined at the parent level can be overruled by the child class. So be careful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function source stored in a string eg.: var source=(function(){return Math.sin(x);}); I want
I have function like this typedef vector<vector<string> > vecArray; vecArray dul(); vecArray dul() {
hello i want i have function to get paragraph of selected text. this is
if i have function f(x,y,z), how to get first derivation of my function in
I have function along these lines: public void view(string msg) { messagebox.show(msg); } .
I have: function send(){ $.get(/site/send.php, function(data){ alert(data); } } In site/send.php I have: sleep(1000)
I have function as below . public static object getClassInstance(string key, object constructorParameter) {
I have function where I want display a number in text. if (all >
I have: function dosomething(data) { alert($(this).attr('class')) $(this).children('img').attr('title', data) } $('.test').bind('mouseover', function() { $.get('/inc/ajax-function.asp?action=check', dosomething);
I have function GoToCell(number); This feature renders the motion of the player. 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.