title: Random meta:

  • name: description content: Used to generate random verification codes, random strings, and more.
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Component Library|Miscellaneous Tools|Random

Random

Use

Used to generate random verification codes, random strings, etc.

Core Object Class

To implement this component function you need to load the core class:

  1. EasySwoole\Utility\Random

Core Object Method

Character

Strings are randomly generated:

  • int $length generation length
  • string $alphabet custom generated character set
  1. static function character($length = 6, $alphabet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789')

Number

Pure numeric strings are randomly generated:

  • int $length generation length
  1. Static function number(length = 6)

ArrayRandOne

Generate an individual randomly from the collection:

  • array $length array collection
  1. Static function arrayRandOne(array $data)

How to use

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. var_dump(\EasySwoole\Utility\Random::character());
  10. var_dump(\EasySwoole\Utility\Random::number());
  11. var_dump(\EasySwoole\Utility\Random::arrayRandOne(['one', 'two', 'three']));
  12. /**
  13. * Output results:
  14. * string(6) "W94ohx"
  15. * string(6) "986543"
  16. * string(3) "two"
  17. */