Creating a Simple Php Functions


Creating a Simple Php Functions

As you program, you'll discover that there are certain sections of code you frequently use, either within a single script or in several. Placing these routines into a self-defined function can save you time and make your programming easier, especially as your Web sites become larger. Once you create a function, the actions of that function take place each time the function is called, just as print() will send text to the browser with each use. The syntax to create a user-defined function is: function FunctionName () { statement(s); }

You can use roughly the same naming conventions for the function name as you do for variables, just without the initial dollar sign. The most important rule is to remain consistent. Second to that is the suggestion of creating meaningful function names, just as you ought to write representative variable names CreateHeader would be a better function name than Function1.

Remember not to use spaces, though, as that would constitute two separate words for the function name, which will result in error messages the underscore is a logical replacement for the space, for example Create_Header is a valid function name.

Any valid PHP code can go within the statements area of the function, including calls to other functions. There is also no limit to the number of statements a function has, but make sure each statement ends with a semi-colon.