3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Length { public $violations = []; /** * Minimum length. * * @var int */ private $min; /** * Maximum length. * * @var int */ private $max; /** * Charset. * * @var string */ private $charset; /** * Length validation rule constructor. * * @param int $min * @param int $max * @param string $charset * @throws InvalidArgumentException * @throws LogicException */ public function __construct( $min = null, $max = null, $charset = 'UTF-8' ) { if ($min === null && $max === null) { throw new \InvalidArgumentException('Either option "min" or "max" must be given.'); } if ($max < $min) { throw new \LogicException('"Max" option cannot be less that "min".'); } $this->min = $min; $this->max = $max; $this->charset = $charset; } /** * Validates input. * * @param mixed $input * * @return bool */ public function isValid($input = null) { if ($input === null || $input === '') { return false; } $input = (string) $input; if (!$invalidCharset = !@mb_check_encoding($input, $this->charset)) { $length = mb_strlen($input, $this->charset); } if ($invalidCharset) { $this->violations[] = 'charset'; return false; } if ($this->max !== null && $length > $this->max) { $this->violations[] = 'max'; return false; } if ($this->min !== null && $length < $this->min) { $this->violations[] = 'min'; return false; } return true; } } $length = new Length(-100, ['a', 'b', 'c', 185, null]); $length->isValid(stream_context_create()); echo sprintf('Count fail violations: %d', count($length->violations));
Output for git.master, git.master_jit, rfc.property-hooks
Count fail violations: 0

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
31.91 ms | 401 KiB | 8 Q