Variables

I l @ ve RuBoard

Variables

PHP is described as a loosely typed language. That is, you don't need to declare the type of variables before you can use them. PHP sets out variable types for you. To see this, look at the following code:

 <?php  //our data types  $intdata = 1;  $doubledata = 5.00;  $stringdata = "Andrew";  $booldata = TRUE;  //list data types  $IntType = gettype($intdata);  print("$intdata is a $IntType data type<BR>");  $DoubleType = gettype($doubledata);  print("$doubledata is a $DoubleType data type<BR>");  $StringType = gettype($stringdata);  print("$stringdata is a $StringType data type<BR>");  $BoolType = gettype($booldata);  print("$booldata is a $BoolType data type<BR>");  ?> 

When you store data in a variable, PHP sets the type of variable according to the type of data you are storing. First, you specify what data your variables will hold:

 $intdatavar = 1;  $doubledatavar = 5.00;  $stringvar = "Andrew";  $boolvar = TRUE; 

PHP sets each variable to the type of data you are storing. For example, because you are storing the string "Andrew" in the $stringvar variable, PHP sets the variable to a string type. You can see this by showing what type of data your variables are using through the GetType function:

 $IntType = GetType($intdatavar); 

You then display the variable and its type:

 print("$intdatavar is a $IntType data type<BR>"); 

If you run the script, you will see this working, as shown in Figure 3.2.

Figure 3.2. PHP data types.

graphics/03fig02.gif

Note that the Boolean type is shown as a 1. PHP displays Boolean values as a 1 for true and null (empty value) for false. We will explore this further in the next section.

Setting Data Types

Although loosely typed variables can be useful, it is sometimes necessary to set types explicitly (loosely typed variables can be described as implicitly typed), such as when handling data that has been input from a database. To do this, you use the SetType method:

 <?php  //our data types  $intdata = 300;  $doubledata = 5.00;  $string = "False";  $bool = TRUE;  //list data types  $IntType = GetType($intdata);  Print("$intdata is a $IntType data type<BR>");  $DoubleType = GetType($doubledata);  Print("$doubledata is a $DoubleType data type<BR>");  $StringType = GetType($string);  Print("$string is a $StringType data type<BR>");  $BoolType = GetType($bool);  Print("$bool is a $BoolType data type<BR>");  //change data types  SetType($doubledata, "integer");  SetType($intdata, "double");  SetType($bool, "string");  SetType($string, "boolean");  //display new types  $IntType = GetType($intdata);  Print("$intdata is now a $IntType data type<BR>");  $DoubleType = GetType($doubledata);  Print("$doubledata is now a $DoubleType data type<BR>");  $StringType = GetType($string);  Print("$string is now a $StringType data type<BR>");  $BoolType = GetType($bool);  Print("$bool is now a $BoolType data type<BR>");  ?> 

As in your first script, you add some content to the variables and display that content and the variables' types:

 $intdata = 300;  $doubledata = 5.00;  $string = "False";  $bool = TRUE;  $IntType = GetType($intdata);  Print("$intdata is a $IntType data type<BR>");  $DoubleType = GetType($doubledata);  Print("$doubledata is a $DoubleType data type<BR>");  $StringType = GetType($string);  Print("$string is a $StringType data type<BR>");  $BoolType = GetType($bool);  Print("$bool is a $BoolType data type<BR>"); 

You can then change the type of each variable using the SetType function:

 SetType($doubledata, "integer");  SetType($intdata, "double");  SetType($bool, "string");  SetType($string, "Boolean"); 

Within the SetType function, you first state what variable you want to change (such as $doubledata ) and then what type you want to change the variable to (such as integer). To complete the script, you display the variable contents and type:

 $IntType = GetType($intdata);  Print("$intdata is now a $IntType data type<BR>");  $DoubleType = GetType($doubledata);  Print("$doubledata is now a $DoubleType data type<BR>");  $StringType = GetType($string);  Print("$string is now a $StringType data type<BR>");  $BoolType = GetType($bool);  Print("$bool is now a $BoolType data type<BR>"); 

If you run the script, you should see that the data types have been altered as you set them in the script, as shown in Figure 3.3.

Figure 3.3. Using the SetType function to change variable types.

graphics/03fig03.gif

Although changing integer and double types is no problem, converting Boolean and string data types presents a bigger challenge. In the script output, you can see that the string output is displayed as a 1. According to the rules of how PHP displays Boolean values, this is correct, because PHP displays true values as 1, so this converts directly into a string as a value of 1.

Also in the script output, you can see that the Boolean output is displayed as a 1. You converted the False string to Boolean, so, according to PHP, the Boolean output should be empty. This is sadly not the case; PHP in fact sees anything other than a null string or 0 value as being a true Boolean value. You can show this by setting the string in the script to a value of 0:

 $string = "0"; 

If you run the script (see Figure 3.4), you can see that the Boolean value is now displayed as an empty value. Remember that PHP sees a 0 value as a Boolean false value, so when you convert a 0 value set as a string to a Boolean, that value becomes a Boolean false value. Also remember that PHP displays Boolean false values as null values, so you get an empty value displayed when you run the script.

Figure 3.4. Data with a false Boolean value.

graphics/03fig04.gif

Typecasting

Typecasting gives you another method of setting variable types.

 <?php  $somedata = 6.23;  $newtype = GetType($somedata);  Print("$somedata is set by PHP to be a $newtype data type with the value of graphics/ccc.gif $somedata<BR><BR>");  //change to string  $changetype = (string) $somedata;  $newtype = GetType($changetype);  Print("$somedata is now a $newtype data type with the value of $changetype<BR>");  //change to int  $changetype = (integer) $somedata;  $newtype = GetType($changetype);  Print("$somedata is now a $newtype data type with the value of $changetype<BR>");  //change to double  $changetype = (double) $somedata;  $newtype = GetType($changetype);  Print("$somedata is now a $NewType data type with the value of $ChangeType<BR>");  //change to boolean  $changetype = (boolean) $somedata;  $newtype = GetType($changetype);  Print("$somedata is now a $newtype data type with the value of $changetype<BR>");  ?> 

First, you give a variable a value:

 $somedata = 6.23; 

You know that, given this value, PHP will set its type for you to a double. You can, however, change it at will. If you want to set it to a string, you can use the following:

 $changetype = (string) $somedata; 

Here the variable $changetype takes the value of the data variable $somedata but assumes string as its data type. You can do this with all the PHP data types, as the script shows. If you run the script, you can see typecasting at work, as shown in Figure 3.5.

Figure 3.5. Using the ChangeType function for typecasting.

graphics/03fig05.gif

In the script, you can see that the data value of 6.23 has been set by PHP to be a double data type. As you typecast the variable to each of the PHP data types, you can see how the variable value changes in accordance.

Arithmetic Operators

PHP lets you use arithmetic operators on compatible variable types. What variable types are these? Any variable type that can hold data in a numeric form; this means any variable type other than Boolean. The following example also uses string concatenation (joining strings). Don't worry if you are unfamiliar with this. We will look at it in further detail in a moment.

 <?php  //numbers to use  $num1 = 10;  $num2 = 20;  //addition  $add_sum = $num1 + $num2;  Print($num1 . " + " . $num2 . " = " . $add_sum);  print("<BR>");  //subtraction  $sub_sum = $num2 - $num1;  Print($num2 . " - " . $num1 . " = " . $sub_sum);  print("<BR>");  //division  $div_sum = $num2 / $num1;  Print($num2 . " / " . $num1 . " = " . $div_sum);  print("<BR>");  //multiplication  $mup_sum = $num2 * $num1;  Print($num2 . " * " . $num1 . " = " . $mup_sum);  print("<BR>");  //modulus  $mod_sum = $num2 % $num1;  Print($num2 . " % " . $num1 . " = " . $mod_sum);  print("<BR>");  ?> 

First, you set two variables that will hold values for you to perform arithmetic operations on:

 $num1 = 10;  $num2 = 20; 

Next, you perform an arithmetic operation on the two variables:

 $add_sum = $num1 + $num2; 

Finally, you display the result of the arithmetic operation:

 Print("$num1 + $num2 = $add_sum"); 

If you run this script, you can see the result of the various arithmetic operations, as shown in Figure 3.6.

Figure 3.6. Arithmetic operators.

graphics/03fig06.gif

It is interesting to note that the following also works in this script:

 $num1 = 10;  $num2 = "20"; 

Here you make each variable a mixed type. $num1 is an integer type, and $num2 is a string type. If you change the variables to the following, the script still works:

 $num1 = 10;  $num2 = "aa20"; 

The script still runs, but the arithmetic operators fail to work. If PHP sees data other than numeric data in a variable, it ignores that variable.

String Operators and Functions

PHP provides you with several methods for working with string variables. Quite often you use string variables from sources such as HTML forms, files, and databases, so PHP provides many methods for manipulating this kind of data.

We will begin our look at string methods by defining some strings to work with:

 $first_name = "Fox";  $space = " ";  $last_name = "Mulder"; 
Joining Strings

To join these strings, you use PHP's concatenation operator:

 $name = $first_name . $space . $last_name; 

Here you create a new string by joining the three original strings. Note that you use a dot character to join strings.

Length of a String

You can find out the length of the new string using the strlen function:

 $string_length = strlen($name); 
Trailing Space

PHP lets you remove a trailing space (often called white space) from either the start and end of a string or just the start of a string. Because the new string has no trailing space, you must add it to be able to apply these functions:

 $space_name = " $name "; 

You can then remove it from the start of the string using the ltrim function:

 $ltrim_name = ltrim($space_name); 

You can remove the trailing space from both the start and the end of the string using the trim function:

 $trim_name = trim($space_name); 
String Case

PHP lets you change a string's case to either all uppercase or all lowercase. To change the string to uppercase, you use the strtoupper function:

 $uppercase_name = strtoupper($name); 

To change the string to lowercase, you use the strtolower function:

 $lowercase_name = strtolower($name); 
Substrings

PHP lets you find the position of a substring, create a substring, and add substrings to a string. To create a substring, you use the substr function:

 $sub_string = substr($name, 3); 

Here you specify the string you want to create the substring from (in this case, the string $name ) and the position within the string to create the substring from (in this case, from the third character to the end of the string).

You can find the position of a substring using the strops function:

 $substr_position = strpos($name, $sub_string); 

Here you specify the string in which to locate the substring's position (in this case, the string $name ) and the substring whose position is to be located (in this case, the substring you created earlier: $sub_string ).

Finally, you can add a substring to a string using the str_replace function:

 $sub_name = str_replace("Fox", "Scully", $name); 

In the str_replace function, you first specify which string is to be found (in this case, "Fox" ). What string you will replace it with (in this case, "Scully" ) and what string you will be working on (in this case, $name ) come next.

Testing String Methods

You can test all these methods using the following script:

 <?php  //strings  $first_name = "Fox";  $space = " ";  $last_name = "Mulder";  //joining  $name = $first_name . $space . $last_name;  print("$name is our joined string");  print("<BR>");  //length  $string_length = strlen($name);  print("The length of the joined string is $string_length");  print("<BR>");  //trailing space  $space_name = " $name ";  print("A string trailing space is *$space_name*");  print("<BR>");  $ltrim_name = ltrim($space_name);  print("A string with leading trailing space removed is *$ltrim_name*");  print("<BR>");  $trim_name = trim($space_name);  print("A string with trailing space removed is *$trim_name*");  print("<BR>");  //case  $uppercase_name = strtoupper($name);  print("An uppercase string is $uppercase_name");  print("<BR>");  $lowercase_name = strtolower($name);  print("A lowercase string is $lowercase_name");  print("<BR>");  //substrings  //substring  $sub_string = substr($name, 3);  print("A substring is $sub_string");  print("<BR>");  //substr position  $substr_position = strpos($name, $sub_string);  print("The substring position is $substr_position");  print("<BR>");  //replace substr  $sub_name = str_replace("Fox", "Scully", $name);  print("An inserted substring is $sub_name");  print("<BR>");  ?> 

If you run this script, you can see how all the string methods work, as shown in Figure 3.7. Note that when showing the concatenation method, I have surrounded the string with a * character. This is to show the trailing space between that character and the string.

Figure 3.7. String methods.

graphics/03fig07.gif

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net