Variables defined within a function are by definition local to the function. Unlike Perl, global variables are not within the scope of a function unless explicitly declared with the global keyword as on line 18. Another PHP keyword, static , indicates a variable should retain its value between function invocations. The example in a nutshell PHP documents can switch back and forth between HTML and PHP as often as your logic dictates. However, a PHP document always begins in HTML mode unless explicitly stated to the contrary.
Good programming employs a decomposition methodology.
A problem is partitioned into smaller and smaller logical components until it cannot be broken down any further. Ideally, each component in the program's main line should be a call to a subprogram. Following this procedure makes even complex programs easy to follow and helps with the goal of program maintainability. The demo script calls itself when the user clicks on the form's submit button. The script must therefore determine if the access is the first time or a subsequent pass.