PHP Functions & Control Structures

As you are well aware, a function is made up of a number of statements that are executed one at a time, but as a single unit. Functions should be constructed so that they do one things. In other words, your function should calculate a mortgage payment (principal and interest) but it should not calculate a mortgage payment, the monthly taxes, or the monthly insurance escrow amounts. These last two items should be calculated in their own functions with all of the functions contributing to a variable named something like $totalPayment. As you saw in last week’s study material, a PHP function looks like this: 

<?php function_name ($parameter1, $parameter2) {   Put PHP function statements here;} ?> 

Doesn’t this look similar to JavaScript (JS)? If you answered in the affirmative, you’re on the right track. Like JS, in PHP, function statements are placed inside curly braces and end in a semi-colon. 

Once you create a function, you need to invoke (or call) it. In PHP v4 and later, you can place PHP code at the bottom of the page and call the function from the top of the page. I don’t know why you’d do that – don’t! – but it can be done. As in Visual Basic, variables can have limited or global scope. A global variable has scope for that Web page whereas a standard variable’s scope is limited to the specific function in which it was created. 

PHP includes various predefined global arrays, called autoglobals or superglobals, which contain client, server, and environment information that you can use in your scripts. Autoglobals are arrays whose elements are referred to (when needed) with an alphanumeric key instead of an index number. Two of the most commonly used autoglobals are $_GET and $_POST. They allow you to access the values of forms that are submitted to a PHP script. The default method for submitting a form is “get,” which appends form data as one long string to the URL specified by the action attribute (this data is actually displayed in the browser’s address bar). The “post” option sends form data as a transmission separate from the URL specified by the action attribute.For example, if we have a form set up like this:

<form action=”start.php” method=”get”>Name: <input type=”text” name=”fname” />Age: <input type=”text” name=”lname” /><input type=”submit” />

</form>

When the user clicks submit, on the next Web page that we display, the address bar will look like this:

http://www.goblackwood.com/start.php?fname=John&lname=Blackwood

So, what do you think? Should you use $_GET to pass usernames and passwords between your Web pages? NOT!The Web page we display after the user clicks submit could have the following code to use these values to display what the user previously entered:

Welcome <?php echo $_GET[”fname”] . “ “ . echo $_GET[”lname”]; ?>.<br />

The $_POST variable collects values from a form invisibly! This means that when the user clicks submit, the data you pick up from the form can’t be seen in the address bar and is invisible. Here’s what the address bar looks like when the user clicks submit and we next open the start.php Web page:http://www.goblackwood.com/start.phpNow, the start.php Web page can use the $_POST variable to pick up what the user previously entered:

Welcome <?php echo $_POST[”fname”] . “ “ . echo $_POST[”lname”]; ?>.<br />

Why use POST instead of GET? Simply put – you don’t want values entered by a user displayed in the address bar. It’s that easy.

One more superglobal should be mentioned, although it’s not mentioned in the chapter. The $_COOKIE variable is used to retrieve a cookie value. A cookie is a small file put on a user’s computer by your Web server. Then, so long as the cookie is “alive” the cookie can be used to identify a user each time the user requests one of your Web pages. You can also use a cookie to customize a user’s experience on your Web page, just as Amazon.com does when you are greeted with your name when you visit their site after having previously logged in.

You create a cookie using the setcookie() function in PHP. Next, you obtain the cookie values (such as the user’s name, pages visited, etc.) by printing or echoing the value to the screen, paper, email, or a database. Contrary to popular opinion, cookies cannot harm your computer.

As expected, PHP supports IF, IF-ELSE, nested IF, and SWITCH statements. Further, when you need to perform the same code statement many times (as in a VB for statement), you can use PHP’s while, do…while, for, and foreach statements.

When a looping statement (such as a while statement) repeats itself, this is known as an iteration. Remember that when you loop, you must ensure that the loop ends. Otherwise, the loop will run forever.

The while statement does something so long as the condition is true. The do…while statement does something at least once and iterates so long as the condition remains true while the do statement iterates one time only. The for statement iterates so long as your condition and counter remain true. Finally, foreach statements are used to iterate through arrays or objects (like folders).

Links you may find useful for this week’s study:

http://www.w3schools.com/PHP/php_functions.asp http://www.w3schools.com/PHP/php_if_else.asp http://www.w3schools.com/PHP/php_looping.asp http://www.w3schools.com/PHP/php_get.asp http://www.w3schools.com/PHP/php_cookies.asp

Leave a Reply

You must be logged in to post a comment.