3v4l.org

run code in 300+ PHP versions simultaneously
<?php // define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) {$nameErr = "Name is required";} else { $name = test_input($_POST["name"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) {$emailErr = "Email is required";} else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "Invalid email format"; } } if (empty($_POST["website"])) {$website = "";} else { $website = test_input($_POST["website"]); // check if URL address syntax is valid (this regular expression also allows dashes in the URL) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["comment"])) {$comment = "";} else {$comment = test_input($_POST["comment"]);} if (empty($_POST["gender"])) {$genderErr = "Gender is required";} else {$gender = test_input($_POST["gender"]);} } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br> Website: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br><br> Gender: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 94
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 75
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 87
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
2 jumps found. (Code = 46) Position 1 = 118, Position 2 = 120
Branch analysis from position: 118
2 jumps found. (Code = 43) Position 1 = 121, Position 2 = 122
Branch analysis from position: 121
2 jumps found. (Code = 46) Position 1 = 125, Position 2 = 127
Branch analysis from position: 125
2 jumps found. (Code = 43) Position 1 = 128, Position 2 = 129
Branch analysis from position: 128
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 129
Branch analysis from position: 127
Branch analysis from position: 122
Branch analysis from position: 120
Branch analysis from position: 87
2 jumps found. (Code = 46) Position 1 = 118, Position 2 = 120
Branch analysis from position: 118
Branch analysis from position: 120
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 87
Branch analysis from position: 85
Branch analysis from position: 87
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 70
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 75
Branch analysis from position: 73
Branch analysis from position: 75
Branch analysis from position: 70
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 51
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
Branch analysis from position: 56
Branch analysis from position: 51
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 32
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
Branch analysis from position: 37
Branch analysis from position: 32
Branch analysis from position: 94
filename:       /in/4nkpO
function name:  (null)
number of ops:  143
compiled vars:  !0 = $nameErr, !1 = $emailErr, !2 = $genderErr, !3 = $websiteErr, !4 = $name, !5 = $email, !6 = $gender, !7 = $comment, !8 = $website
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                           ~9      !3, ''
          1        ASSIGN                                           ~10     !2, ~9
          2        ASSIGN                                           ~11     !1, ~10
          3        ASSIGN                                                   !0, ~11
    4     4        ASSIGN                                           ~13     !8, ''
          5        ASSIGN                                           ~14     !7, ~13
          6        ASSIGN                                           ~15     !6, ~14
          7        ASSIGN                                           ~16     !5, ~15
          8        ASSIGN                                                   !4, ~16
    6     9        FETCH_R                      global              ~18     '_SERVER'
         10        FETCH_DIM_R                                      ~19     ~18, 'REQUEST_METHOD'
         11        IS_EQUAL                                                 ~19, 'POST'
         12      > JMPZ                                                     ~20, ->94
    8    13    >   FETCH_IS                                         ~21     '_POST'
         14        ISSET_ISEMPTY_DIM_OBJ                         1          ~21, 'name'
         15      > JMPZ                                                     ~22, ->18
    9    16    >   ASSIGN                                                   !0, 'Name+is+required'
         17      > JMP                                                      ->32
   12    18    >   INIT_FCALL_BY_NAME                                       'test_input'
         19        CHECK_FUNC_ARG                                           
         20        FETCH_FUNC_ARG               global              $24     '_POST'
         21        FETCH_DIM_FUNC_ARG                               $25     $24, 'name'
         22        SEND_FUNC_ARG                                            $25
         23        DO_FCALL                                      0  $26     
         24        ASSIGN                                                   !4, $26
   14    25        INIT_FCALL                                               'preg_match'
         26        SEND_VAL                                                 '%2F%5E%5Ba-zA-Z+%5D%2A%24%2F'
         27        SEND_VAR                                                 !4
         28        DO_ICALL                                         $28     
         29        BOOL_NOT                                         ~29     $28
         30      > JMPZ                                                     ~29, ->32
   16    31    >   ASSIGN                                                   !0, 'Only+letters+and+white+space+allowed'
   20    32    >   FETCH_IS                                         ~31     '_POST'
         33        ISSET_ISEMPTY_DIM_OBJ                         1          ~31, 'email'
         34      > JMPZ                                                     ~32, ->37
   21    35    >   ASSIGN                                                   !1, 'Email+is+required'
         36      > JMP                                                      ->51
   24    37    >   INIT_FCALL_BY_NAME                                       'test_input'
         38        CHECK_FUNC_ARG                                           
         39        FETCH_FUNC_ARG               global              $34     '_POST'
         40        FETCH_DIM_FUNC_ARG                               $35     $34, 'email'
         41        SEND_FUNC_ARG                                            $35
         42        DO_FCALL                                      0  $36     
         43        ASSIGN                                                   !5, $36
   26    44        INIT_FCALL                                               'preg_match'
         45        SEND_VAL                                                 '%2F%28%5B%5Cw%5C-%5D%2B%5C%40%5B%5Cw%5C-%5D%2B%5C.%5B%5Cw%5C-%5D%2B%29%2F'
         46        SEND_VAR                                                 !5
         47        DO_ICALL                                         $38     
         48        BOOL_NOT                                         ~39     $38
         49      > JMPZ                                                     ~39, ->51
   28    50    >   ASSIGN                                                   !1, 'Invalid+email+format'
   32    51    >   FETCH_IS                                         ~41     '_POST'
         52        ISSET_ISEMPTY_DIM_OBJ                         1          ~41, 'website'
         53      > JMPZ                                                     ~42, ->56
   33    54    >   ASSIGN                                                   !8, ''
         55      > JMP                                                      ->70
   36    56    >   INIT_FCALL_BY_NAME                                       'test_input'
         57        CHECK_FUNC_ARG                                           
         58        FETCH_FUNC_ARG               global              $44     '_POST'
         59        FETCH_DIM_FUNC_ARG                               $45     $44, 'website'
         60        SEND_FUNC_ARG                                            $45
         61        DO_FCALL                                      0  $46     
         62        ASSIGN                                                   !8, $46
   38    63        INIT_FCALL                                               'preg_match'
         64        SEND_VAL                                                 '%2F%5Cb%28%3F%3A%28%3F%3Ahttps%3F%7Cftp%29%3A%5C%2F%5C%2F%7Cwww%5C.%29%5B-a-z0-9%2B%26%40%23%5C%2F%25%3F%3D%7E_%7C%21%3A%2C.%3B%5D%2A%5B-a-z0-9%2B%26%40%23%5C%2F%25%3D%7E_%7C%5D%2Fi'
         65        SEND_VAR                                                 !8
         66        DO_ICALL                                         $48     
         67        BOOL_NOT                                         ~49     $48
         68      > JMPZ                                                     ~49, ->70
   40    69    >   ASSIGN                                                   !3, 'Invalid+URL'
   44    70    >   FETCH_IS                                         ~51     '_POST'
         71        ISSET_ISEMPTY_DIM_OBJ                         1          ~51, 'comment'
         72      > JMPZ                                                     ~52, ->75
   45    73    >   ASSIGN                                                   !7, ''
         74      > JMP                                                      ->82
   47    75    >   INIT_FCALL_BY_NAME                                       'test_input'
         76        CHECK_FUNC_ARG                                           
         77        FETCH_FUNC_ARG               global              $54     '_POST'
         78        FETCH_DIM_FUNC_ARG                               $55     $54, 'comment'
         79        SEND_FUNC_ARG                                            $55
         80        DO_FCALL                                      0  $56     
         81        ASSIGN                                                   !7, $56
   49    82    >   FETCH_IS                                         ~58     '_POST'
         83        ISSET_ISEMPTY_DIM_OBJ                         1          ~58, 'gender'
         84      > JMPZ                                                     ~59, ->87
   50    85    >   ASSIGN                                                   !2, 'Gender+is+required'
         86      > JMP                                                      ->94
   52    87    >   INIT_FCALL_BY_NAME                                       'test_input'
         88        CHECK_FUNC_ARG                                           
         89        FETCH_FUNC_ARG               global              $61     '_POST'
         90        FETCH_DIM_FUNC_ARG                               $62     $61, 'gender'
         91        SEND_FUNC_ARG                                            $62
         92        DO_FCALL                                      0  $63     
         93        ASSIGN                                                   !6, $63
   63    94    >   ECHO                                                     '%0A%3Ch2%3EPHP+Form+Validation+Example%3C%2Fh2%3E%0A%3Cp%3E%3Cspan+class%3D%22error%22%3E%2A+required+field.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cform+method%3D%22post%22+action%3D%22'
   66    95        INIT_FCALL                                               'htmlspecialchars'
         96        FETCH_R                      global              ~65     '_SERVER'
         97        FETCH_DIM_R                                      ~66     ~65, 'PHP_SELF'
         98        SEND_VAL                                                 ~66
         99        DO_ICALL                                         $67     
        100        ECHO                                                     $67
        101        ECHO                                                     '%22%3E+%0A+++Name%3A+%3Cinput+type%3D%22text%22+name%3D%22name%22+value%3D%22'
   67   102        ECHO                                                     !4
        103        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   68   104        ECHO                                                     !0
        105        ECHO                                                     '%3C%2Fspan%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++E-mail%3A+%3Cinput+type%3D%22text%22+name%3D%22email%22+value%3D%22'
   70   106        ECHO                                                     !5
        107        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   71   108        ECHO                                                     !1
        109        ECHO                                                     '%3C%2Fspan%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++Website%3A+%3Cinput+type%3D%22text%22+name%3D%22website%22+value%3D%22'
   73   110        ECHO                                                     !8
        111        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E'
   74   112        ECHO                                                     !3
        113        ECHO                                                     '%3C%2Fspan%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++Comment%3A+%3Ctextarea+name%3D%22comment%22+rows%3D%225%22+cols%3D%2240%22%3E'
   76   114        ECHO                                                     !7
        115        ECHO                                                     '%3C%2Ftextarea%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++Gender%3A%0A+++%3Cinput+type%3D%22radio%22+name%3D%22gender%22+'
   79   116        ISSET_ISEMPTY_CV                                 ~68     !6
        117      > JMPZ_EX                                          ~68     ~68, ->120
        118    >   IS_EQUAL                                         ~69     !6, 'female'
        119        BOOL                                             ~68     ~69
        120    > > JMPZ                                                     ~68, ->122
        121    >   ECHO                                                     'checked'
        122    >   ECHO                                                     '++value%3D%22female%22%3EFemale%0A+++%3Cinput+type%3D%22radio%22+name%3D%22gender%22+'
   80   123        ISSET_ISEMPTY_CV                                 ~70     !6
        124      > JMPZ_EX                                          ~70     ~70, ->127
        125    >   IS_EQUAL                                         ~71     !6, 'male'
        126        BOOL                                             ~70     ~71
        127    > > JMPZ                                                     ~70, ->129
        128    >   ECHO                                                     'checked'
        129    >   ECHO                                                     '++value%3D%22male%22%3EMale%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   81   130        ECHO                                                     !2
        131        ECHO                                                     '%3C%2Fspan%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++%3Cinput+type%3D%22submit%22+name%3D%22submit%22+value%3D%22Submit%22%3E+%0A%3C%2Fform%3E%0A%0A'
   87   132        ECHO                                                     '%3Ch2%3EYour+Input%3A%3C%2Fh2%3E'
   88   133        ECHO                                                     !4
   89   134        ECHO                                                     '%3Cbr%3E'
   90   135        ECHO                                                     !5
   91   136        ECHO                                                     '%3Cbr%3E'
   92   137        ECHO                                                     !8
   93   138        ECHO                                                     '%3Cbr%3E'
   94   139        ECHO                                                     !7
   95   140        ECHO                                                     '%3Cbr%3E'
   96   141        ECHO                                                     !6
   97   142      > RETURN                                                   1

Function test_input:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4nkpO
function name:  test_input
number of ops:  15
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
   57     1        INIT_FCALL                                               'trim'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
   58     5        INIT_FCALL                                               'stripslashes'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !0, $3
   59     9        INIT_FCALL                                               'htmlspecialchars'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $5      
         12        ASSIGN                                                   !0, $5
   60    13      > RETURN                                                   !0
   61    14*     > RETURN                                                   null

End of function test_input

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.5 ms | 1412 KiB | 21 Q