3v4l.org

run code in 300+ PHP versions simultaneously
<?php function validate_length ($options = ['min_length' => 1, 'max_length' => null], $filters = null) { return static function ($value = null) use ($options, $filters) { $len = strlen($value); if (isset($options['min_length']) && $len < $options['min_length']) { return false; } if (isset($options['max_length']) && $len > $options['max_length']) { return false; } if (null !== $filters) { return filter_var($value, ...$filters); //php 5.6+ array spread operator } return $value; }; } $a = [ 'a' => 'value', 'b' => '', 'c' => 'a@example.com', 'd' => 'This& is a 🍕 Test', ]; $email_filter = [FILTER_VALIDATE_EMAIL, ['flags' => FILTER_FLAG_EMAIL_UNICODE]]; $string_filter = [FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_ENCODE_AMP | FILTER_FLAG_STRIP_HIGH]]; $result = filter_var_array ($a, [ 'a' => ['filter' => FILTER_CALLBACK, 'options' => validate_length(['max_length' => 4])], 'b' => ['filter' => FILTER_CALLBACK, 'options' => validate_length(['min_length' => 1])], 'c' => ['filter' => FILTER_CALLBACK, 'options' => validate_length(['min_length' => 1, 'max_length' => 20], $email_filter)], 'd' => ['filter' => FILTER_CALLBACK, 'options' => validate_length(['min_length' => 1], $string_filter)], ]); var_dump($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/83uXu
function name:  (null)
number of ops:  41
compiled vars:  !0 = $a, !1 = $email_filter, !2 = $string_filter, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   ASSIGN                                                   !0, <array>
   30     1        ASSIGN                                                   !1, <array>
   31     2        FETCH_CONSTANT                                   ~6      'FILTER_SANITIZE_STRING'
          3        INIT_ARRAY                                       ~7      ~6
          4        ADD_ARRAY_ELEMENT                                ~7      <array>
          5        ASSIGN                                                   !2, ~7
   33     6        INIT_FCALL                                               'filter_var_array'
          7        SEND_VAR                                                 !0
   34     8        INIT_ARRAY                                       ~9      1024, 'filter'
          9        INIT_FCALL                                               'validate_length'
         10        SEND_VAL                                                 <array>
         11        DO_FCALL                                      0  $10     
         12        ADD_ARRAY_ELEMENT                                ~9      $10, 'options'
         13        INIT_ARRAY                                       ~11     ~9, 'a'
         14        INIT_ARRAY                                       ~12     1024, 'filter'
   35    15        INIT_FCALL                                               'validate_length'
         16        SEND_VAL                                                 <array>
         17        DO_FCALL                                      0  $13     
         18        ADD_ARRAY_ELEMENT                                ~12     $13, 'options'
         19        ADD_ARRAY_ELEMENT                                ~11     ~12, 'b'
   34    20        INIT_ARRAY                                       ~14     1024, 'filter'
   36    21        INIT_FCALL                                               'validate_length'
         22        SEND_VAL                                                 <array>
         23        SEND_VAR                                                 !1
         24        DO_FCALL                                      0  $15     
         25        ADD_ARRAY_ELEMENT                                ~14     $15, 'options'
         26        ADD_ARRAY_ELEMENT                                ~11     ~14, 'c'
   34    27        INIT_ARRAY                                       ~16     1024, 'filter'
   37    28        INIT_FCALL                                               'validate_length'
         29        SEND_VAL                                                 <array>
         30        SEND_VAR                                                 !2
         31        DO_FCALL                                      0  $17     
         32        ADD_ARRAY_ELEMENT                                ~16     $17, 'options'
         33        ADD_ARRAY_ELEMENT                                ~11     ~16, 'd'
         34        SEND_VAL                                                 ~11
   33    35        DO_ICALL                                         $18     
         36        ASSIGN                                                   !3, $18
   40    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAR                                                 !3
         39        DO_ICALL                                                 
         40      > RETURN                                                   1

Function validate_length:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/83uXu
function name:  validate_length
number of ops:  7
compiled vars:  !0 = $options, !1 = $filters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_INIT                                        !0      <array>
          1        RECV_INIT                                        !1      null
    5     2        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          3        BIND_LEXICAL                                             ~2, !0
          4        BIND_LEXICAL                                             ~2, !1
   18     5      > RETURN                                                   ~2
   19     6*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 10
filename:       /in/83uXu
function name:  {closure}
number of ops:  29
compiled vars:  !0 = $value, !1 = $options, !2 = $filters, !3 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV_INIT                                        !0      null
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
    6     3        STRLEN                                           ~4      !0
          4        ASSIGN                                                   !3, ~4
    7     5        ISSET_ISEMPTY_DIM_OBJ                         0  ~6      !1, 'min_length'
          6      > JMPZ_EX                                          ~6      ~6, ->10
          7    >   FETCH_DIM_R                                      ~7      !1, 'min_length'
          8        IS_SMALLER                                       ~8      !3, ~7
          9        BOOL                                             ~6      ~8
         10    > > JMPZ                                                     ~6, ->12
    8    11    > > RETURN                                                   <false>
   10    12    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !1, 'max_length'
         13      > JMPZ_EX                                          ~9      ~9, ->17
         14    >   FETCH_DIM_R                                      ~10     !1, 'max_length'
         15        IS_SMALLER                                       ~11     ~10, !3
         16        BOOL                                             ~9      ~11
         17    > > JMPZ                                                     ~9, ->19
   11    18    > > RETURN                                                   <false>
   13    19    >   TYPE_CHECK                                  1020          !2
         20      > JMPZ                                                     ~12, ->27
   14    21    >   INIT_FCALL                                               'filter_var'
         22        SEND_VAR                                                 !0
         23        SEND_UNPACK                                              !2
         24        CHECK_UNDEF_ARGS                                         
         25        DO_ICALL                                         $13     
         26      > RETURN                                                   $13
   17    27    > > RETURN                                                   !0
   18    28*     > RETURN                                                   null

End of Dynamic Function 0

End of function validate_length

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.78 ms | 1440 KiB | 20 Q