This is a series of tutorials; here are the others:
This exercise assumes that you either have PHP installed on your computer already or that you have access to a server with PHP support.
Hail, reader! It is gratifying to find that you enjoyed (or at least survived) my first tutorial on the subject of PHP and preferred to continue with the course! I applaud this effort, as it is in the direction of one of the most useful and essential languages to a web developer in the Web 2.0 era.
But we shall hold the accolades until the end. For now, lets get a brief overview of what we’ll be digging into in Part II:
- Variables and their uses
- Data types
- New functions
- Arithmetic operators
- Variables and their uses
- Data types
- Arithmetic Operators
- #1: Using the gettype() function on $x when $x = “4.3” will return…
- #2: Using $x from Question 1, using the settype() function on $x, changing it to Integer, will return the value…
- #3: Which of the following is not an arithmetic operator?
- #4: The data type NULL is present when a variable…
- #5: Variables are always preceded by…
Variables and their uses
Variables are the underlying dynamic ability of PHP. It’s what allows code to modify the input of a user to suit the purpose of the code’s author. Now, if you’ve taken anything higher than Algebra, you will find this section to be something of a breeze. For those who feel that a review would be beneficial, however, I’ve decided to include a crash course in variables and their operations.
Variables in PHP are much the same in nature as those in math: it is a number, sequence, or operator defined by a letter or a name, such as x or variable_1. In order to define a variable in PHP, you simply type the name of your variable and precede the name by a dollar sign, $. Following this, you will proceed to define the variable with an equal sign similar to below:
$name = "Matt Lidlum";
You will notice that the variable is now defined as Matt Lidlum, which is the argument of the function. The argument, if you’ll remember, is whatever is between either the parentheses or else the quotation marks. And as before, it is important to note that the variable is not defined as “Matt Lidlum”, but simply Matt Lidlum.
Names, however, are not the only things that you can put in here. Numbers can go in as well, as well as operators (which will be discussed later), and various other data types. For now, play around with variables and try using the print function we learned in Part I. An example script is below:
<html> <head> <title>PHP for Beginners: Part II</title> </head> <body> <?php $name = "Matt Lidlum"; print "$name"; // This function will print Matt Lidlum ?> </body> </html>
Data types will again call on your mathematical skills in order for you to gain full comprehension. You will find a number of references to math class, so hopefully your motivation to learn PHP and other languages will be motivation enough to keep your head off that comfy sweatshirt.
Back to the matter at hand, there are a total of eight data types used within PHP: six standard and two special. To familiarize yourself with them, please glance over the following tables:
Standard Data Types
Special Data Types
As I’m sure you will notice, a number of these data types are already familiar to you. The print function that we discussed within Part I as well as our $name variable both used a string data type. We will not be stopping there, however. Later this lesson you will also touch base on the double, integer, boolean, and NULL data types as well within the functions section.
As a quick review, you will remember that last lesson we learnt the function print. Within the argument of the print function you could type anything that you would wish to be displayed on your web page. Moving on from this, we will be mastering three new functions, all of which are more used as diagnostic tools to find errors in the code.
This function is similar to gettype() in its purpose: diagnostics. It does much the same thing, but in a different way. By placing a variable’s name within the argument, PHP will post the variable’s type as well as the variable’s value. Again, the purpose of this function will be built upon later.
Settype() is used to change the variable’s type. This function comes in handy when evaluating a user’s input. For example, let’s say that you have created a form based off of PHP and HTML that asks for a user’s input on the company’s customer service. From a drop-down menu, they may choose either good, bad, or impartial. Now, for instance, let’s say that the user chooses good. With settype we can rewrite that variable as true, a boolean data type as you may remember. Thus, in later parts of the code, the true value will initiate other code segments to execute. Another example is a rating system. In a similar question, suppose the user has the option of typing a number 1-5. Suppose now that they type a non-integer value such as 4.2. With settype, the author can automatically change that input to an integer by simply setting the new type of the variable from double to integer. Thus, 4.2 will round down to 4, giving the author exactly what they want. In order for this to be carried out, however, the author must complete the function in the following manner:
<?php $VAR_NAME = "VAR_VALUE" settype ( $VAR_NAME , NEW_DATA_TYPE) ?>
Now that you have three new functions to add to your print function, it’s time to give them all a shot. Try using variables and messing around with data types as well. Below is an example of PHP and HTML code that I drew up:
<html> <head> <title>PHP for Beginners: Part II</title> </head> <body> <?php $double = "4.5"; $integer = "6"; $name = "Matt Lidlum"; //Three separate variables print "$name" //Matt Lidlum gettype ($integer) //Integer gettype ($name) //String settype ($double, int) //$double is now an integer print "$double" //5 ?> </body> </html>
Okay, I know this is getting long and a lot is being thrown at you, but hang in there. What the following discusses is operators; the little things that allow you to manipulate data such as variables or input. Below is a table of arithmetic operators. There are a number of other operators within PHP that are used to do such things as compare data, but we will focus on those in a later tutorial:
I’m sure you are heaving a huge sigh of relief because as you can tell, these are very simple operators. Simple operators, however, that can have a significant effect on a piece of code. We’ll start off nice and easy with operators. First, lets try them with the print function:
<?php print "4 + 5"; //9 ?>
Nothing to it, right? This is, as the section is titled, simple arithmetic. Now lets try it out with variables and even changing the variable’s value.
<?php $x = "8"; $x = "$x + 4"; //$x now equals 12 $y = "2 * $x"; //$y now equals 24 $z = "$y - $x"; //$z now equals 12 print "$y - $x is equal to $z!"; //Math is fun! ?>
Here, I defined $x, modified it by adding 4, then defined $y as 2 * $x, and finally made $z equal to $y – $x. Play around with these operators, mixing the print or any other new function among it.
Exams help you to retain knowledge that you have just gained, so with that in mind, I’m afraid it’s back to the classroom for everyone. I urge you to not look back at the material or peek ahead at the answer key.
#1: Using the gettype() function on $x when $x = “4.3” will return…
#2: Using $x from Question 1, using the settype() function on $x, changing it to Integer, will return the value…
#3: Which of the following is not an arithmetic operator?
#4: The data type NULL is present when a variable…
A.) Has not been used in a function
B.) Has not been initiated (defined)
C.) Is placed in the gettype() function
#5: Variables are always preceded by…
Answer key: C,C,B,B,A
Well everyone, this tutorial must now come to a close! We’ve made some great strides in part two, and I hope that you’ll stick around for the next article, PHP for Beginners: Part III.