3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Password { /** * @var integer desired length of password, default: null */ protected $length; /** * @var array $numbers available numbers (0-9), default: [] */ protected $numbers = []; /** * @var array $uppercase available uppercase characters (A-Z), default: [] */ protected $uppercase = []; /** * @var array available lowercase characters (a-z), default: [] */ protected $lowercase = []; /** * @var array available symbols (! @ # $ % ^ * - _ + = ?), default: [] */ protected $symbols = []; /** * @var array characters to exclude from available characters - supersedes include, default: [] */ protected $exclude = []; /** * @var array characters to include from available characters - overridden by exclude, default: [] */ protected $include = []; /** * @var int amount of times to repeat the specified characters, default: 1 */ protected $repeat = 1; /** * @var array list of characters to exclude when symbols is selected by default */ protected $extended = ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}']; /** * @var array default values */ protected $options = [ 'numbers' => true, 'uppercase' => true, 'lowercase' => false, 'symbols' => false, 'extended' => false, 'exclude' => [], 'include' => [], 'repeat' => 1 ]; /** * @var bool generate as html, default: false */ protected $html = false; /** * generates a password of the specified length using desired options * attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters * automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached * @param integer $length desired length of password * @param bool $numbers include numbers (0-9) in available characters, default: true * @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true * @param bool $lowercase include lowercase characters (a-z) in available characters, default: false * @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false * @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false * @param array $exclude characters to exclude from available characters - supersedes include, default: array() * @param array $include characters to include from available characters - overridden by exclude, default: array() * @param integer $repeat how many times to repeat the specified characters, default: 1 * @return \ISEL\Auth\Password */ public function __construct($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null) { $this->setLength($length); $this->setOptions([ 'numbers' => $numbers, 'uppercase' => $uppercase, 'lowercase' => $lowercase, 'symbols' => $symbols, 'extended' => $extended, 'exclude' => $exclude, 'include' => $include, 'repeat' => $repeat ]); } /** * generates a password of the specified length using desired options * attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters * automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached * @param integer $length desired length of password * @param bool $numbers include numbers (0-9) in available characters, default: true * @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true * @param bool $lowercase include lowercase characters (a-z) in available characters, default: false * @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false * @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false * @param array $exclude characters to exclude from available characters - supersedes include, default: array() * @param array $include characters to include from available characters - overridden by exclude, default: array() * @param integer $repeat how many times to repeat the specified characters, default: 1 * @return string */ public static function factory($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null) { return new self($length, $numbers, $uppercase, $lowercase, $symbols, $extended, $exclude, $include, $repeat); } /** * @param array $options set options using an array of values, and use defaults */ public function setOptions(array $options) { foreach ($this->options as $option => $default) { $method = 'set' . ucfirst($option); $value = (true === isset($options[$option]) && null !== $options[$option] ? $options[$option] : $default); $this->$method($value); } } /** * convert password string to html friendly string * @param $password * @return string */ public static function passwordToHtml($password) { return htmlentities($password, ENT_HTML5 | ENT_QUOTES, false); } /** * convert html entity string to literal string * @param $password * @return string */ public static function passwordFromHtml($password) { return html_entity_decode($password, ENT_HTML5 | ENT_QUOTES); } /** * convert password string to html friendly string * @param null $password * @return string */ public function toHtml($password = null) { return self::passwordToHtml($password ? : $this->generate()); } /** * takes an html entity string and converts it to literal characters * @param $password * @return string */ public function fromHtml($password) { return self::passwordFromHtml($password); } /** * output the password based on specified settings * automatically adjusts based on supplied characters and desired length * @return string */ public function output() { $characters = $this->getAvailableCharacters(); $string = substr(str_shuffle(str_repeat($characters, (ceil($this->length / strlen($characters)) * $this->repeat))), 0, $this->length); if (true === empty($string)) { throw new \RuntimeException('No string generated, check excluded characters.'); } if (true === $this->html) { $string = self::passwordToHtml($string); } return $string; } /** * @return string */ public function getAvailableCharacters() { return implode('', array_diff(array_merge($this->include, $this->lowercase, $this->uppercase, $this->numbers, $this->symbols), array_merge($this->exclude, $this->extended))); } /** * set Exclude * @param array $exclude * @return $this */ public function setExclude(array $exclude) { $this->exclude = $exclude; return $this; } /** * set Html * @param boolean $html * @return $this */ public function setHtml($html) { $this->html = $html; return $this; } /** * set Include * @param array $include * @return $this */ public function setInclude(array $include) { $this->include = $include; return $this; } /** * set Length * @param int $length * @return $this */ public function setLength($length) { $this->length = (int) $length; return $this; } /** * set Lowercase * @param boolean $lowercase * @return $this */ public function setLowercase($lowercase, $start = 'a', $end = 'z') { $this->lowercase = (true === (bool) $lowercase ? range(strtolower($start), strtolower($end)) : []); return $this; } /** * set Numbers * @param boolean $numbers * @return $this */ public function setNumbers($numbers, $start = 0, $end = 9) { $this->numbers = (true === (bool) $numbers ? range((int) $start, (int) $end) : []); return $this; } /** * set Repeat * @param int $repeat * @return $this */ public function setRepeat($repeat) { $this->repeat = (int) $repeat; return $this; } /** * set Symbols * @param boolean $symbols include symbols * @return $this */ public function setSymbols($symbols) { $this->symbols = (true === (bool) $symbols ? array_merge(range('!', '/'), range(':', '@'), range('[', '`')) : []); return $this; } /** * set Extended * @param bool $extended include extended symbols, default: false * @return $this */ public function setExtended($extended) { $this->extended = (false === (bool) $extended ? ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}'] : []); return $this; } /** * set Uppercase * @param boolean $uppercase * @param string $start starting character, default: A * @param string $end ending character, default: Z * @return $this */ public function setUppercase($uppercase, $start = 'A', $end = 'Z') { $this->uppercase = (true === (bool) $uppercase ? range(strtoupper($start), strtoupper($end)) : []); return $this; } /** * @return string */ public function __toString() { return $this->output(); } } $temppasword = Password::factory(20490)->output(); echo $temppasword;

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.4.00.0040.01515.16
7.3.120.0110.00614.91
7.3.110.0030.01515.16
7.3.100.0000.01614.96
7.3.90.0040.01114.99
7.3.80.0000.01014.72
7.3.70.0090.00614.87
7.3.60.0100.00315.05
7.3.50.0090.00614.89
7.3.40.0000.01615.01
7.3.30.0000.01315.03
7.3.20.0100.00316.46
7.3.10.0030.00916.43
7.3.00.0040.01216.82
7.2.250.0070.01015.08
7.2.240.0130.00615.03
7.2.230.0060.00915.46
7.2.220.0030.01015.36
7.2.210.0090.00614.75
7.2.200.0000.01515.40
7.2.190.0060.01015.25
7.2.180.0060.00915.11
7.2.170.0040.01114.98
7.2.60.0070.00416.84
7.1.330.0070.01015.96
7.1.320.0070.01315.65
7.1.310.0100.00315.75
7.1.300.0060.00916.04
7.1.290.0060.00615.61
7.1.280.0110.00715.96
7.1.270.0000.01415.99
7.1.260.0040.00815.63
7.1.200.0030.01015.70
7.1.70.0030.00917.09
7.1.60.0000.02219.32
7.1.50.0030.02016.96
7.0.200.0000.00916.78

preferences:
39.92 ms | 401 KiB | 5 Q