3v4l.org

run code in 300+ PHP versions simultaneously
<!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?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; ?> </body> </html>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 95
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 38
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 57
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 76
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 83
Branch analysis from position: 83
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 88
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 95
Branch analysis from position: 95
2 jumps found. (Code = 46) Position 1 = 119, Position 2 = 121
Branch analysis from position: 119
2 jumps found. (Code = 43) Position 1 = 122, Position 2 = 123
Branch analysis from position: 122
2 jumps found. (Code = 46) Position 1 = 126, Position 2 = 128
Branch analysis from position: 126
2 jumps found. (Code = 43) Position 1 = 129, Position 2 = 130
Branch analysis from position: 129
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 130
Branch analysis from position: 128
Branch analysis from position: 123
Branch analysis from position: 121
Branch analysis from position: 88
2 jumps found. (Code = 46) Position 1 = 119, Position 2 = 121
Branch analysis from position: 119
Branch analysis from position: 121
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 88
Branch analysis from position: 86
Branch analysis from position: 88
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 71
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 76
Branch analysis from position: 74
Branch analysis from position: 76
Branch analysis from position: 71
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 52
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 57
Branch analysis from position: 55
Branch analysis from position: 57
Branch analysis from position: 52
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 33
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 38
Branch analysis from position: 36
Branch analysis from position: 38
Branch analysis from position: 33
Branch analysis from position: 95
filename:       /in/kAkcI
function name:  (null)
number of ops:  145
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
-------------------------------------------------------------------------------------
    1     0  E >   ECHO                                                     '%3C%21DOCTYPE+HTML%3E+%0A%3Chtml%3E%0A%3Chead%3E%0A%3Cstyle%3E%0A.error+%7Bcolor%3A+%23FF0000%3B%7D%0A%3C%2Fstyle%3E%0A%3C%2Fhead%3E%0A%3Cbody%3E+%0A%0A'
   12     1        ASSIGN                                           ~9      !3, ''
          2        ASSIGN                                           ~10     !2, ~9
          3        ASSIGN                                           ~11     !1, ~10
          4        ASSIGN                                                   !0, ~11
   13     5        ASSIGN                                           ~13     !8, ''
          6        ASSIGN                                           ~14     !7, ~13
          7        ASSIGN                                           ~15     !6, ~14
          8        ASSIGN                                           ~16     !5, ~15
          9        ASSIGN                                                   !4, ~16
   15    10        FETCH_R                      global              ~18     '_SERVER'
         11        FETCH_DIM_R                                      ~19     ~18, 'REQUEST_METHOD'
         12        IS_EQUAL                                                 ~19, 'POST'
         13      > JMPZ                                                     ~20, ->95
   17    14    >   FETCH_IS                                         ~21     '_POST'
         15        ISSET_ISEMPTY_DIM_OBJ                         1          ~21, 'name'
         16      > JMPZ                                                     ~22, ->19
   18    17    >   ASSIGN                                                   !0, 'Name+is+required'
         18      > JMP                                                      ->33
   21    19    >   INIT_FCALL_BY_NAME                                       'test_input'
         20        CHECK_FUNC_ARG                                           
         21        FETCH_FUNC_ARG               global              $24     '_POST'
         22        FETCH_DIM_FUNC_ARG                               $25     $24, 'name'
         23        SEND_FUNC_ARG                                            $25
         24        DO_FCALL                                      0  $26     
         25        ASSIGN                                                   !4, $26
   23    26        INIT_FCALL                                               'preg_match'
         27        SEND_VAL                                                 '%2F%5E%5Ba-zA-Z+%5D%2A%24%2F'
         28        SEND_VAR                                                 !4
         29        DO_ICALL                                         $28     
         30        BOOL_NOT                                         ~29     $28
         31      > JMPZ                                                     ~29, ->33
   25    32    >   ASSIGN                                                   !0, 'Only+letters+and+white+space+allowed'
   29    33    >   FETCH_IS                                         ~31     '_POST'
         34        ISSET_ISEMPTY_DIM_OBJ                         1          ~31, 'email'
         35      > JMPZ                                                     ~32, ->38
   30    36    >   ASSIGN                                                   !1, 'Email+is+required'
         37      > JMP                                                      ->52
   33    38    >   INIT_FCALL_BY_NAME                                       'test_input'
         39        CHECK_FUNC_ARG                                           
         40        FETCH_FUNC_ARG               global              $34     '_POST'
         41        FETCH_DIM_FUNC_ARG                               $35     $34, 'email'
         42        SEND_FUNC_ARG                                            $35
         43        DO_FCALL                                      0  $36     
         44        ASSIGN                                                   !5, $36
   35    45        INIT_FCALL                                               'preg_match'
         46        SEND_VAL                                                 '%2F%28%5B%5Cw%5C-%5D%2B%5C%40%5B%5Cw%5C-%5D%2B%5C.%5B%5Cw%5C-%5D%2B%29%2F'
         47        SEND_VAR                                                 !5
         48        DO_ICALL                                         $38     
         49        BOOL_NOT                                         ~39     $38
         50      > JMPZ                                                     ~39, ->52
   37    51    >   ASSIGN                                                   !1, 'Invalid+email+format'
   41    52    >   FETCH_IS                                         ~41     '_POST'
         53        ISSET_ISEMPTY_DIM_OBJ                         1          ~41, 'website'
         54      > JMPZ                                                     ~42, ->57
   42    55    >   ASSIGN                                                   !8, ''
         56      > JMP                                                      ->71
   45    57    >   INIT_FCALL_BY_NAME                                       'test_input'
         58        CHECK_FUNC_ARG                                           
         59        FETCH_FUNC_ARG               global              $44     '_POST'
         60        FETCH_DIM_FUNC_ARG                               $45     $44, 'website'
         61        SEND_FUNC_ARG                                            $45
         62        DO_FCALL                                      0  $46     
         63        ASSIGN                                                   !8, $46
   47    64        INIT_FCALL                                               'preg_match'
         65        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'
         66        SEND_VAR                                                 !8
         67        DO_ICALL                                         $48     
         68        BOOL_NOT                                         ~49     $48
         69      > JMPZ                                                     ~49, ->71
   49    70    >   ASSIGN                                                   !3, 'Invalid+URL'
   53    71    >   FETCH_IS                                         ~51     '_POST'
         72        ISSET_ISEMPTY_DIM_OBJ                         1          ~51, 'comment'
         73      > JMPZ                                                     ~52, ->76
   54    74    >   ASSIGN                                                   !7, ''
         75      > JMP                                                      ->83
   56    76    >   INIT_FCALL_BY_NAME                                       'test_input'
         77        CHECK_FUNC_ARG                                           
         78        FETCH_FUNC_ARG               global              $54     '_POST'
         79        FETCH_DIM_FUNC_ARG                               $55     $54, 'comment'
         80        SEND_FUNC_ARG                                            $55
         81        DO_FCALL                                      0  $56     
         82        ASSIGN                                                   !7, $56
   58    83    >   FETCH_IS                                         ~58     '_POST'
         84        ISSET_ISEMPTY_DIM_OBJ                         1          ~58, 'gender'
         85      > JMPZ                                                     ~59, ->88
   59    86    >   ASSIGN                                                   !2, 'Gender+is+required'
         87      > JMP                                                      ->95
   61    88    >   INIT_FCALL_BY_NAME                                       'test_input'
         89        CHECK_FUNC_ARG                                           
         90        FETCH_FUNC_ARG               global              $61     '_POST'
         91        FETCH_DIM_FUNC_ARG                               $62     $61, 'gender'
         92        SEND_FUNC_ARG                                            $62
         93        DO_FCALL                                      0  $63     
         94        ASSIGN                                                   !6, $63
   72    95    >   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'
   75    96        INIT_FCALL                                               'htmlspecialchars'
         97        FETCH_R                      global              ~65     '_SERVER'
         98        FETCH_DIM_R                                      ~66     ~65, 'PHP_SELF'
         99        SEND_VAL                                                 ~66
        100        DO_ICALL                                         $67     
        101        ECHO                                                     $67
        102        ECHO                                                     '%22%3E+%0A+++Name%3A+%3Cinput+type%3D%22text%22+name%3D%22name%22+value%3D%22'
   76   103        ECHO                                                     !4
        104        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   77   105        ECHO                                                     !0
        106        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'
   79   107        ECHO                                                     !5
        108        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   80   109        ECHO                                                     !1
        110        ECHO                                                     '%3C%2Fspan%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++Website%3A+%3Cinput+type%3D%22text%22+name%3D%22website%22+value%3D%22'
   82   111        ECHO                                                     !8
        112        ECHO                                                     '%22%3E%0A+++%3Cspan+class%3D%22error%22%3E'
   83   113        ECHO                                                     !3
        114        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'
   85   115        ECHO                                                     !7
        116        ECHO                                                     '%3C%2Ftextarea%3E%0A+++%3Cbr%3E%3Cbr%3E%0A+++Gender%3A%0A+++%3Cinput+type%3D%22radio%22+name%3D%22gender%22+'
   88   117        ISSET_ISEMPTY_CV                                 ~68     !6
        118      > JMPZ_EX                                          ~68     ~68, ->121
        119    >   IS_EQUAL                                         ~69     !6, 'female'
        120        BOOL                                             ~68     ~69
        121    > > JMPZ                                                     ~68, ->123
        122    >   ECHO                                                     'checked'
        123    >   ECHO                                                     '++value%3D%22female%22%3EFemale%0A+++%3Cinput+type%3D%22radio%22+name%3D%22gender%22+'
   89   124        ISSET_ISEMPTY_CV                                 ~70     !6
        125      > JMPZ_EX                                          ~70     ~70, ->128
        126    >   IS_EQUAL                                         ~71     !6, 'male'
        127        BOOL                                             ~70     ~71
        128    > > JMPZ                                                     ~70, ->130
        129    >   ECHO                                                     'checked'
        130    >   ECHO                                                     '++value%3D%22male%22%3EMale%0A+++%3Cspan+class%3D%22error%22%3E%2A+'
   90   131        ECHO                                                     !2
        132        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'
   96   133        ECHO                                                     '%3Ch2%3EYour+Input%3A%3C%2Fh2%3E'
   97   134        ECHO                                                     !4
   98   135        ECHO                                                     '%3Cbr%3E'
   99   136        ECHO                                                     !5
  100   137        ECHO                                                     '%3Cbr%3E'
  101   138        ECHO                                                     !8
  102   139        ECHO                                                     '%3Cbr%3E'
  103   140        ECHO                                                     !7
  104   141        ECHO                                                     '%3Cbr%3E'
  105   142        ECHO                                                     !6
  107   143        ECHO                                                     '%0A%3C%2Fbody%3E%0A%3C%2Fhtml%3E'
  109   144      > RETURN                                                   1

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

End of function test_input

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.83 ms | 1412 KiB | 21 Q