PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

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 statement(s) 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 .

The formatting of a function is not important as long as the requisite elements are there. These elements include the word function , the function's name, the opening and closing parentheses, the opening and closing braces, and the statement(s).It is conventional to indent a function's statement(s) from the previous line (as I have done), for clarity's sake. In any case, select a format style that you like (which is both syntactically correct and logically sound) and stick to it.

You call (or enact) the function by referring to it just as you do any built-in function. The line of code FunctionName(); will cause the statement(s); part of the predefined function above to be executed.

Let's begin by rewriting the password- generating script from Chapter 5, Using Strings , as its own function.

To create and call a basic function:

  1. Open passwords.php in your text editor (Script 9.1).

    Script 9.1. This is the original passwords script that doesn't use functions. You'll create your first function using the code at the heart of this page.

  2. Make the first line of the page <?php, as opposed to the standard beginning of the HTML header (Script 9.2, next page).

    To insure that the function is created before it is called (which is required in PHP 3), I'll write the function as the very first part of the script, even before any HTML code.

  3. function CreatePassword () {

    The name of the function will be CreatePassword which is representative of what the function does and will consequently be easy to remember.

  4. Now place the PHP code (lines 613) from passwords.php into the function. As indicated in Script 9.2 I would recommend placing these lines indented from the function name to indicate they belong to the function.

    Script 9.2. Placing the function at the very beginning of the script is a good way to separate it out. Likewise, indenting the function's statements help to indicate their relationship to the function. Then, in the main body of your script, one-line of code executes the function's many statements.

    $String = "This is the text which will be encrypted so that we may create random and secure passwords!"; $Length = 8; $String = md5($String); $StringLength = strlen($String); srand ((double) microtime() * 1000000); $Begin = rand(0,($StringLength- $Length-1)); $Password = substr ($String, $Begin, $Length); print ("Your recommended password is: <P><BIG>$Password</BIG><P>\n");

  5. Close the function on the next line with a curly brace : }.

    Omitting the opening or closing brace is another common cause of errors so be sure to follow the syntax carefully .

  6. Close the PHP code with ?>.

    Since you are now going to write the basic HTML part of the page, you need to close the PHP section. You could also keep the PHP section open and then use print() to send the HTML to the browser, if you so desire , in which case you would skip this step.

  7. Write the common HTML heading code,

    <HTML><HEAD><TITLE>Password Generator within a Function</TITLE><BODY>.

  8. Begin a new PHP section of the page with <?php.

    You can actually open and close several sections of PHP code within an HTML document. It is common for programmers to do so.

  9. CreatePassword();

    Once you've created your function, you simply have to call it by name (being careful to use the exact spelling) to make the function work. Be sure to include the parentheses as well.

  10. ?></BODY></HTML>

    Close the second PHP part and finish the HTML.

  11. Save your script, upload it to the server, and test it in your Web browser (Figure 9.1).

    Figure 9.1. By placing the several PHP lines of password.php into one function, you've begun to modularize your code. This is the first step towards making dynamic Web sites even if the obvious visual result is the same as it was before you created the function.

Tip

If the server you are working on is running PHP 3, you must define the function before calling it. Although this is not true in PHP 4, I would recommend that you habitually set your functions at the beginning of a script, insuring they will always be created before being called.

Tip

Function names for the functions you create are case-insensitive, just as existing PHP functions are case-insensitive. Therefore it won't matter in your code if you use createpassword instead of the more proper CreatePassword .

I l @ ve RuBoard

Категории