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

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

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?

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 edited Aug 29, 2018 at 11:21 Fayaz 9,0172 gold badges33 silver badges51 bronze badges asked Aug 28, 2018 at 12:22 Juri RudiJuri Rudi 3012 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17 +50

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744921780a4601178.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信