Archive for December, 2008

Working with Data Types and Operators

Monday, December 15th, 2008

You should be quite familiar with variables at this point in your programming education and recall that variable data types determine what types of operations it supports along with the amount of memory which is allocated to support it.

For example, a Boolean variable typically would not be used to store a user name or password. Its values are binary and these values are either true (1) or false (0). You would use a Boolean variable to determine whether something happened (or not) or whether something is on (true) or off (false).

Another example of a data type is an array. Arrays, as you should recall, are great for collecting data through the use of a single variable for related items. For example, let’s say you were creating a list of automobile models. You could create an array that stores each model for Ford trucks and then refer to this one variable, along with its index rather than defining a named variable for each one. Don’t forget that an array’s index begins with zero (0) and that this identifies the first item in the array. The array is an example of a compound data type. This means that it can hold more than one value. The other type of compound data type is the object, which you worked with in VB (and can be used in PHP).

You’ve spent a lot of time working with what are called primitive data types. A primitive data type is a variable that can hold one value. Examples are strings, integers, floating point numbers, and Booleans.

In PHP, the main things to remember are that variable names (referred to as identifiers) must begin with a dollar sign and that variable names cannot contain any spaces. While some use underscores to separate words in a variable name, I personally don’t like doing that. I prefer to use something like $LastName rather than $last_name because to me the underscore takes longer. But, that’s just me. You can use whichever you like.

A nice online review of PHP data types can be found at: http://www.daaq.net/old/php/index.php?page=php+data+types&parent=php+basics.

 

Assignment operators (+=, -+, *=, /=, =), comparisons, logical operators (AND, OR, XOR), and math operators work the same in PHP as they do in VB.  One operator does need an explanation though, and it is the modulus. The modulus operation differs from simple division and this is easy to see through the use of a math calculation. If we divide 7/3 we get 2.3333333333 (it goes on forever). The modulus is the remainder (.3333333333).

 

Type casting, or casting, ensures that a data type is the kind a script expects. This is helpful for form input, verifying that data is indeed of the kind the script needs to execute. You can use the gettype() function to view a variable’s data type, and other functions to determine whether a variable is of a specific data type.

It’s probably a good time to review the operator order of precedence. In other words, when you have a math formula such as 7 x (8 + 3) / 4 x 2^3, what is done first? How do we always know the correct answer? I always remember the order in which operations are evaluated by using the sentence: Please Excuse My Dear Aunt Sally. Taking the first letter of each word, we can gleam the order. Here’s the answer: Parentheses, Exponents, Multiplication, Division, Addition, and Subtraction. So, you always add before you subtract, perform calculations inside parentheses before doing anything else, multiply before adding, etc.

 

Feel free to drop me an email if you’d like the answer to the problem above.

PHP Functions & Control Structures

Sunday, December 7th, 2008

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