What's the difference between WordPress random_int() and PHP built-in function random_int()?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
7
down vote

favorite












What's the difference between WordPress defined function random_int() and PHP built-in function random_int()?



Also, if there is a difference, how does the PHP interpreter understand which of the two functions I'm calling?







share|improve this question




























    up vote
    7
    down vote

    favorite












    What's the difference between WordPress defined function random_int() and PHP built-in function random_int()?



    Also, if there is a difference, how does the PHP interpreter understand which of the two functions I'm calling?







    share|improve this question
























      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      What's the difference between WordPress defined function random_int() and PHP built-in function random_int()?



      Also, if there is a difference, how does the PHP interpreter understand which of the two functions I'm calling?







      share|improve this question














      What's the difference between WordPress defined function random_int() and PHP built-in function random_int()?



      Also, if there is a difference, how does the PHP interpreter understand which of the two functions I'm calling?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 29 at 11:21









      Fayaz

      5,13811228




      5,13811228










      asked Aug 28 at 12:22









      Juri Rudi

      695




      695




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          13
          down vote



          accepted










          WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented.




          how does the PHP interpreter understand which of the two functions I'm calling?




          Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this function new, you would get an error. This is why. the file with the function definition is only loaded, when random_int() is not available by default.



          wp-includes/random_compat/random.php lines 212-214 are



          if (!function_exists('random_int')) 
          require_once $RandomCompatDIR.'/random_int.php';



          • So if your server is PHP7 and PHP's own random_int() is callable, this one is used and the file never included.


          • If your server is not PHP7 or PHP's own random_int() is not callable for any reasons, the file is included and another implementation will be given.


          This is done, so WordPress can run on different systems. Those with PHP5 and those with PHP7.



          How is it different? I can't really speak to it. The files WP uses seem to be from this random_compat repository, which is also linked from the PHP doc (and suggested if you don't have PHP's own implementation available).



          One quick difference that I already saw: PHP's random_int() tries to use getrandom(2) on Linux machines, while the compat random_int() only uses /dev/urandom.



          When in doubt, I would use the system's versions (PHP) instead of those introduced by software (WP). But that is only my opinion.






          share|improve this answer






















          • "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
            – Mast
            Aug 28 at 18:19











          • @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
            – kero
            Aug 29 at 10:10






          • 1




            You don't recommend going the extra miles, ok. That's all I wanted to know :-)
            – Mast
            Aug 29 at 10:23










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "110"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f312710%2fwhats-the-difference-between-wordpress-random-int-and-php-built-in-function-r%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          13
          down vote



          accepted










          WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented.




          how does the PHP interpreter understand which of the two functions I'm calling?




          Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this function new, you would get an error. This is why. the file with the function definition is only loaded, when random_int() is not available by default.



          wp-includes/random_compat/random.php lines 212-214 are



          if (!function_exists('random_int')) 
          require_once $RandomCompatDIR.'/random_int.php';



          • So if your server is PHP7 and PHP's own random_int() is callable, this one is used and the file never included.


          • If your server is not PHP7 or PHP's own random_int() is not callable for any reasons, the file is included and another implementation will be given.


          This is done, so WordPress can run on different systems. Those with PHP5 and those with PHP7.



          How is it different? I can't really speak to it. The files WP uses seem to be from this random_compat repository, which is also linked from the PHP doc (and suggested if you don't have PHP's own implementation available).



          One quick difference that I already saw: PHP's random_int() tries to use getrandom(2) on Linux machines, while the compat random_int() only uses /dev/urandom.



          When in doubt, I would use the system's versions (PHP) instead of those introduced by software (WP). But that is only my opinion.






          share|improve this answer






















          • "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
            – Mast
            Aug 28 at 18:19











          • @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
            – kero
            Aug 29 at 10:10






          • 1




            You don't recommend going the extra miles, ok. That's all I wanted to know :-)
            – Mast
            Aug 29 at 10:23














          up vote
          13
          down vote



          accepted










          WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented.




          how does the PHP interpreter understand which of the two functions I'm calling?




          Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this function new, you would get an error. This is why. the file with the function definition is only loaded, when random_int() is not available by default.



          wp-includes/random_compat/random.php lines 212-214 are



          if (!function_exists('random_int')) 
          require_once $RandomCompatDIR.'/random_int.php';



          • So if your server is PHP7 and PHP's own random_int() is callable, this one is used and the file never included.


          • If your server is not PHP7 or PHP's own random_int() is not callable for any reasons, the file is included and another implementation will be given.


          This is done, so WordPress can run on different systems. Those with PHP5 and those with PHP7.



          How is it different? I can't really speak to it. The files WP uses seem to be from this random_compat repository, which is also linked from the PHP doc (and suggested if you don't have PHP's own implementation available).



          One quick difference that I already saw: PHP's random_int() tries to use getrandom(2) on Linux machines, while the compat random_int() only uses /dev/urandom.



          When in doubt, I would use the system's versions (PHP) instead of those introduced by software (WP). But that is only my opinion.






          share|improve this answer






















          • "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
            – Mast
            Aug 28 at 18:19











          • @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
            – kero
            Aug 29 at 10:10






          • 1




            You don't recommend going the extra miles, ok. That's all I wanted to know :-)
            – Mast
            Aug 29 at 10:23












          up vote
          13
          down vote



          accepted







          up vote
          13
          down vote



          accepted






          WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented.




          how does the PHP interpreter understand which of the two functions I'm calling?




          Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this function new, you would get an error. This is why. the file with the function definition is only loaded, when random_int() is not available by default.



          wp-includes/random_compat/random.php lines 212-214 are



          if (!function_exists('random_int')) 
          require_once $RandomCompatDIR.'/random_int.php';



          • So if your server is PHP7 and PHP's own random_int() is callable, this one is used and the file never included.


          • If your server is not PHP7 or PHP's own random_int() is not callable for any reasons, the file is included and another implementation will be given.


          This is done, so WordPress can run on different systems. Those with PHP5 and those with PHP7.



          How is it different? I can't really speak to it. The files WP uses seem to be from this random_compat repository, which is also linked from the PHP doc (and suggested if you don't have PHP's own implementation available).



          One quick difference that I already saw: PHP's random_int() tries to use getrandom(2) on Linux machines, while the compat random_int() only uses /dev/urandom.



          When in doubt, I would use the system's versions (PHP) instead of those introduced by software (WP). But that is only my opinion.






          share|improve this answer














          WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented.




          how does the PHP interpreter understand which of the two functions I'm calling?




          Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this function new, you would get an error. This is why. the file with the function definition is only loaded, when random_int() is not available by default.



          wp-includes/random_compat/random.php lines 212-214 are



          if (!function_exists('random_int')) 
          require_once $RandomCompatDIR.'/random_int.php';



          • So if your server is PHP7 and PHP's own random_int() is callable, this one is used and the file never included.


          • If your server is not PHP7 or PHP's own random_int() is not callable for any reasons, the file is included and another implementation will be given.


          This is done, so WordPress can run on different systems. Those with PHP5 and those with PHP7.



          How is it different? I can't really speak to it. The files WP uses seem to be from this random_compat repository, which is also linked from the PHP doc (and suggested if you don't have PHP's own implementation available).



          One quick difference that I already saw: PHP's random_int() tries to use getrandom(2) on Linux machines, while the compat random_int() only uses /dev/urandom.



          When in doubt, I would use the system's versions (PHP) instead of those introduced by software (WP). But that is only my opinion.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 28 at 14:02

























          answered Aug 28 at 12:36









          kero

          2,6771823




          2,6771823











          • "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
            – Mast
            Aug 28 at 18:19











          • @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
            – kero
            Aug 29 at 10:10






          • 1




            You don't recommend going the extra miles, ok. That's all I wanted to know :-)
            – Mast
            Aug 29 at 10:23
















          • "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
            – Mast
            Aug 28 at 18:19











          • @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
            – kero
            Aug 29 at 10:10






          • 1




            You don't recommend going the extra miles, ok. That's all I wanted to know :-)
            – Mast
            Aug 29 at 10:23















          "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
          – Mast
          Aug 28 at 18:19





          "When in doubt, I would use the system's versions" It sounds like you explain at the start of your answer that you can't choose. Either it's already in there (PHP >= 7) and WP won't load it, or it's not there so WP will load it. Is there a middle ground where you pick yourself (disabling WP's function explicitly, I presume)? Sure, you can make any modification you want. But is it worth it to go non-standard in what seems like a relatively core function?
          – Mast
          Aug 28 at 18:19













          @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
          – kero
          Aug 29 at 10:10




          @Mast There's always a way. You could load that repository and simply change the function names (random_int_custom() eg) for it to be usable. The last comment was rather meant as a: it makes sense that it works this way (rather using system functions than your own), and I wouldn't recommend going the extra miles to circumvent this
          – kero
          Aug 29 at 10:10




          1




          1




          You don't recommend going the extra miles, ok. That's all I wanted to know :-)
          – Mast
          Aug 29 at 10:23




          You don't recommend going the extra miles, ok. That's all I wanted to know :-)
          – Mast
          Aug 29 at 10:23

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f312710%2fwhats-the-difference-between-wordpress-random-int-and-php-built-in-function-r%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery