View Single Post
Old 11-23-09, 10:43 PM   #3
Join Date: Mar 2004
Posts: 15,488
Thumbs up Re: Official Learn PHP/MySQL Thread

Part 3 - Variable Types and Operators

First off, I'd like to apologize for having been away from this tutorial for so long. I know a lot of people out there would like to learn PHP and have been talking about the thread, so sorry for the delay. This lesson will consist of mostly just information regarding variable types, getting/changing the variable type, and operators.

Let's start with variable types. For those out there accustomed to other languages (C/C++, C#, etc.) this will be mostly review. The following is a list of variable types in PHP:
  • Boolean - Represents a boolean value. Either 0 (false) or 1 (true)
  • Integer - Number of the set {... -2, -1, 0, 1, 2, ...}
  • Double - (aka Float) Number with a decimal point
  • String - A list of characters, these are typically words, sentences, etc.
  • Array - A vector or matrix consisting of other types of variables
  • Object - Defined by a class, typically contains values and functions (I will go into object-oriented programming and PHP later)
  • Resource - Special variable, holds a reference to an external resource

Now, for the regular programmers out there, you are probably used to declaring the variable type before setting the values. PHP is much easier and tries to guess the variable type you want. Many times PHP is correct, however sometimes it is useful to declare variables before using them. PHP does allow type casting. To do so, just place the type you want the variable to be in parentheses before you set the value. Below are some examples.


$val1 = 123;   // PHP sets $val1 as an integer
$val2 = (string) $val1;  // We instruct PHP to set $val2 as a string with $val1 as the value

There are multiple casts allowed, including (int)/(integer), (bool)/(boolean), (float)/(double), (string), (binary), (array), (object), and (unset) which sets the variable to NULL. For strings, we can also put the value in quotes. For example, in the above, we could have done the following:


$val1 = 123;
$val2 = "$val1";

PHP will interpret $val1 and recognize it as a string and store it into $val2. This is a useful feature, as it allows us to have variable variable names. (this was very useful in some of my projects).


$val1 = "var2";  // Creates $val1 string with value "val2"
$$var1 = 123;  // Creates variable $val2 with value 123

There are also functions available to return string or integer values of a variable. These functions are intval and strval. They will return the integer or string equivalent of a value. We could also do the above via the following.


$val1 = 123;  // Stores 123 (integer) into variable $val1
$val2 = strval($val1);  // Stores string equivalent of $val1, "123" into variable $val2

Finally, if we want to return the variable type, we can use the gettype function. This function returns a string denoting the variable type of the variable passed to it.


$val1 = "this will return string";

If we put the following in a PHP file and put it in our www folder in WAMP, the browser will output the following:

This is useful for checking variable types. Typically, PHP is very good at interpreting what you want to do. For example if we set a string value to 123, then add it to an int, we do get an int that is the sum of the two numbers.


$int = 123;
$stringint = (string) 123;

echo gettype($stringint);
echo "<br />";

The output will read:

Finally, I will cover operators in PHP. These are used to manipulate variables (e.g. numbers) as well as compare values and control logical statements. The mathematical operators are as follows:

Math Operators
  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (returns the remainder)
  • ++ Increment +1
  • -- Increment -1

Assignment Operators
  • = Assign to value
  • += Add then assign (x+=y is the same as x=x+y)
  • -= Subtract then assign
  • *= Multiply then assign
  • /= Divide then assign
  • %= Take modulus then assign
  • .= Append and assign (good for strings)

Comparison Operators
  • == Equals (or equivalent to)
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logic Operators
  • || Or
  • && And
  • ! Not

These are the operators. If you don't understand their usage at the moment, we will cover them. Well that concludes this lesson. I know that I have not really explained arrays very well. Arrays are a very important variable type and are capable of storing almost everything you could ever need in a very simple and logical form.

Next lecture will cover arrays. After that, I will go over IF statements, FOR and WHILE loops, and how to create your own functions in PHP. Hopefully the next lecture won't be 15 months away
ViN86 is offline