Oh dear, I think we have quite a big design problem here.
The first problem I see is that you have extensive use of global variables. Now, globals in PHP are not as bad as in say, C, because a function that uses a global must declare that variable global (unless it is superglobal like $_POST, but those are special anyway). Nonetheless, PHP global variables are still problematic because:
- A reader looking at the code that calls the function has no way of knowing what variables are required and what variables will be modified by the function without inspecting the function's source code. This defeats the advantage of functions in allowing programmers to abstract out portions of code.
- There is tight coupling between functions and their callers. If you want to rename a variable in the calling code, you must perform the same rename in the function, and vice versa.
- As a consequence of #2, the function becomes less reusable. For example, the caller has no way of calling the function with a different set of arguments.
A better way of doing things is to have the functions provide well named parameters. The caller will then pass arguments to these functions, using pass by reference if necessary. That way, a reader will know what variables are needed and may be modified, and at worst only needs to check the parameter list for pass by reference. Furthermore, in PHP 5 you can specify specific types of arguments, leading to some amount of type safety. The caller can rename variables passed, as can the function, and they do not need to rename in sync. The function is reusable since it just needs a different set of arguments.
The second problem that jumps out at me is poor naming of functions. type1() and type2() are not descriptive at all. error1(), error2() etc are likewise not very descriptive. In fact, the errorN() set of functions can probably be refactored into a single function, with a suitable parameter and the use of an array of error messages. With such a setup, instead of having to add a new function if you want to handle a new error, you would just add a new array element. As a by-product of this refactoring, the code will be shortened.