0% found this document useful (0 votes)
8 views

Testing Data Types

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Testing Data Types

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Testing Data Types

Different types of data take up different amounts of memory and may be treated differently when they are
manipulated in a script. Some programming languages therefore demand that the programmer declare in
advance which type of data a variable will contain. By contrast, PHP is loosely typed, meaning that it will
determine the data type at the time data is assigned to each variable.

This automatic typing is a mixed blessing. On the one hand, it means that variables can be used flexibly—in
one instance, a variable can hold a string and then later in the script it can hold an integer or some other data
type. On the other hand, this flexibility can lead to problems in larger scripts if you are specifically expecting a
variable to hold one data type when in fact it holds something completely different. For example, suppose that
you have created code to manipulate an array variable. If the variable in question instead contains a number
value and no array structure is in place, errors will occur when the code attempts to perform array-specific
operations on the variable.

Table 5.1 shows the eight standard data types available in PHP.

Table 5.1. Standard Data Types

Type Example Description

Boolean true One of the special values true or false

Integer 5 A whole number

Float or 3.234 A floating-point number


double

String "hello" A collection of characters

Object An instance of a class

Array An ordered set of keys and values

Resource Reference to a third-party resource (a database, for example)


Type Example Description

NULL An uninitialized variable

PHP has several functions available to test the validity of a particular type of variable—one for each type, in
fact. The is_* family of functions, such as is_bool(), tests whether a given value is a Boolean. Listing 5.1
assigns different data types to a single variable and then tests the variable with the appropriate is_* function.
The comments in the code show you where the script is in the process.

Listing 5.1. Testing the Type of a Variable

1: <?php

2: $testing; // declare without assigning

3: echo "is null? ".is_null($testing); // checks if null

4: echo "<br/>";

5: $testing = 5;

6: echo "is an integer? ".is_int($testing); // checks if integer

7: echo "<br/>";

8: $testing = "five";

9: echo "is a string? ".is_string($testing); // checks if string

10: echo "<br/>";

11: $testing = 5.024;

12: echo "is a double? ".is_double($testing); // checks if double


13: echo "<br/>";

14: $testing = true;

15: echo "is boolean? ".is_bool($testing); // checks if boolean

16: echo "<br/>";

17: $testing = array('apple', 'orange', 'pear');

18: echo "is an array? ".is_array($testing); // checks if array

19: echo "<br/>";

20: echo "is numeric? ".is_numeric($testing); // checks if is numeric

21: echo "<br/>";

22: echo "is a resource? ".is_resource($testing); // checks if is a resource

23: echo "<br/>";

24: echo "is an array? ".is_array($testing); // checks if is an array

25: echo "<br/>";

26: ?>

Put these lines into a text file called testtype.php, and place this file in your web server document root.

When you access this script through your web browser, it produces the following output:

is null? 1

is an integer? 1
is a string? 1

is a double? 1

is boolean? 1

is an array? 1

is numeric?

is a resource?

is an array? 1

When the $testing variable is declared in line 2, no value is assigned to it, so when the variable is tested in
line 3 to see whether it is null (using is_null()), the result is 1 (true). After this, values are assigned
to $testing by using the = sign before testing it with the appropriate is_* function. An integer, assigned to
the $testing variable in line 5, is a whole or real number. In simple terms, you can think of a whole number
as a number without a decimal point. A string, assigned to the $testing variable in line 8, is a collection of

characters. When you work with strings in your scripts, they should always be surrounded by double or single
quotation marks (" or '). A double, assigned to the $testing variable in line 11, is a floating-point number
(that is, a number that includes a decimal point). A Boolean, assigned to the $testing variable in line 14, can
have one of two special values: true or false. In line 17, an array is created using the array() function,

which you’ll learn more about in Chapter 8, “Working with Arrays.” This particular array contains three items,
and the script dutifully reports $testing to have a type of “array.”
From line 20 through the end of the script, no value is reassigned to $testing—only the type is tested. Lines
20 and 22 test whether $testing is a numeric or resource type, respectively, and because it is not, no value
is displayed to the user. In line 24, the script tests again to see whether $testing is an array, and because it
is, the value of 1 is displayed.
Changing Type with settype()

PHP also provides the function settype(), which is used to change the type of a variable. To
use settype(), you place the variable to change and the type to change it to between the parentheses and

separate the elements with a comma, like this:

settype($variabletochange, 'new type');

Listing 5.2 converts the value 3.14 (a float) to each of the four standard types..
Listing 5.2. Changing the Type of a Variable with settype()

1: <?php

2: $undecided = 3.14;

3: echo "is ".$undecided." a double? ".is_double($undecided)."<br/>"; // double

4: settype($undecided, 'string');

5: echo "is ".$undecided." a string? ".is_string($undecided)."<br/>"; // string

6: settype($undecided, 'integer');

7: echo "is ".$undecided." an integer? ".is_integer($undecided)."<br/>"; // int

8: settype($undecided, 'double');

9: echo "is ".$undecided." a double? ".is_double($undecided)."<br/>"; // double

10: settype($undecided, 'bool');

11: echo "is ".$undecided." a boolean? ".is_bool($undecided)."<br/>"; // boolean


12: ?>

In each case, we use the appropriate is_* function to confirm the new data type and to print the value of the
variable $undecided to the browser using echo. When we convert the string "3.14" to an integer in line 6,
any information beyond the decimal point is lost forever. That’s why $undecided contains 3 after we change
it back to a double in line 8. Finally, in line 10, we convert $undecided to a Boolean. Any number other
than 0 becomes true when converted to a Boolean. When printing a Boolean in PHP, true is represented
as 1 and false is represented as an empty string, so in line 11, $undecided is printed as 1.
Put these lines into a text file called settype.php and place this file in your web server document root. When

you access this script through your web browser, it produces the following output:

is 3.14 a double? 1

is 3.14 a string? 1

is 3 an integer? 1

is 3 a double? 1

is 1 a boolean? 1

Changing Type by Casting


The principal difference between using settype() to change the type of an existing variable and changing

type by casting is the fact that casting produces a copy, leaving the original variable untouched. To change
type through casting, you indicate the name of a data type, in parentheses, in front of the variable you are
copying. For example, the following line creates a copy of the $originalvar variable, with a specific type
(integer) and a new name $newvar. The $originalvar variable will still be available, and will be its original
type; $newvar is a completely new variable.

$newvar = (integer) $originalvar


Listing 5.3 illustrates changing data types through casting.

Listing 5.3. Casting a Variable

1: <?php

2: $undecided = 3.14;

3: $holder = (double) $undecided;

4: echo "is ".$holder." a double? ".is_double($holder)."<br/>"; // double

5: $holder = (string) $undecided;

6: echo "is ".$holder." a string? ".is_string($holder)."<br/>"; // string

7: $holder = (integer) $undecided;

8: echo "is ".$holder." an integer? ".is_integer($holder)."<br/>"; // integer

9: $holder = (double) $undecided;

10: echo "is ".$holder." a double? ".is_double($holder)."<br/>"; // double

11: $holder = (boolean) $undecided;

12: echo "is ".$holder." a boolean? ".is_bool($holder)."<br/>"; // boolean

13: echo "<hr/>";

14: echo "original variable type of $undecided: ";

15: echo gettype($undecided); // double

16: ?>
Listing 5.3 never actually changes the type of the $undecided variable, which remains a double throughout
this script, as illustrated on line 15, where the gettype() function is used to determine the type
of $undecided.
In fact, casting $undecided creates a copy that is then converted to the type specified at the time of the cast,
and stored in the variable $holder. This casting occurs first in line 3, and again in lines 5, 7, 9, and 11.
Because the code is working with only a copy of $undecided and not the original variable, it never lost its
original value, as the $undecided variable did in line 6 of Listing 5.2 when its type changed from a string to

an integer.
Put the contents of Listing 5.3 into a text file called casttype.php and place this file in your web server

document root. When you access this script through your web browser, it produces the following output:

is 3.14 a double? 1

is 3.14 a string? 1

is 3 an integer? 1

is 3.14 a double? 1

is 1 a boolean? 1

original variable type of 3.14: double

Now that you’ve seen how to change the contents of a variable from one type to another either by
using settype() or by casting, consider why this might be useful. It is not a procedure that you will have to

use often because PHP automatically casts your variables for you when the context of the script requires a
change. However, such an automatic cast is temporary, and you might want to make a variable persistently
hold a particular data type—thus, the ability to specifically change types.
For example, the numbers that a user types into an HTML form will be made available to your script as the
string type. If you try to add two strings together because they contain numbers, PHP will helpfully convert
these strings into numbers while the addition is taking place. So "30cm" + "40cm"

results in an answer of 70.

During the casting of a string into an integer or float, PHP will ignore any non-numeric characters. The string
will be truncated, and any characters from the location of the first non-numeric character onward are ignored.
So, whereas "30cm" is transformed into "30", the string "6ft2in" becomes just 6 because the rest of the

string evaluates to zero.


You might want to clean up the user input yourself and use it in a particular way in your script. Imagine that the
user has been asked to submit a number. We can simulate this by declaring a variable and assigning the user’s
input to it:

$test = "30cm";

As you can see, the user has added units to his number—instead of entering "30", the user has
entered "30cm". You can make sure that the user input is clean by casting it as an integer:

$newtest = (integer) $test;

echo "Your imaginary box has a width of $newtest centimeters.";

The resulting output would be

Your imaginary box has a width of 30 centimeters.

Had the user input not been cast, and the value of the original variable, $test, been used in place
of $newtest when printing the statement regarding the width of a box, the result would have been

Your imaginary box has a width of 30cm centimeters.

This output looks strange; in fact, it looks like parroted user input that hadn’t been cleaned up (which is exactly
what it is).

Example”
The settype() function is a built-in function in PHP. The settype() function is
used to the set the type of a variable. It is used to set type or modify type of an
existing variable.
Syntax:
boolean settype($variable_name, $type)

Parameters: The settype() function accepts two parameters as shown in above


syntax and are described below.
1. $variable_name: This parameter specifies the name of variable
whose type we want to modify. This parameter can be of any type that is, it
can be of integer type or a string type etc.
2. $type: This parameter specifies the type of variable that is needed. Possible
values of this parameter are: boolean, integer, float, string, array, object,
null.
Return value: This function returns a boolean type value. It returns TRUE in
case of success and FALSE in case of failure.
Below programs illustrate the settype() function in PHP:

Program 1:

<?php

// PHP program to illustrate settype() function

$var1 = "123xyz";

$var2 = 3;

$r = true;

settype($var1, "integer");

settype($var2, "float");
settype($r, "string");

echo $var1."\n";

echo $var2."\n";

echo $r."\n";

?>

Output:
123
3
1
Program 2:

<?php

// PHP program to illustrate settype() function

$var1 = "a12b";

$var2 = 3.566;

$r = true;

settype($var1, "integer");

settype($var2, "integer");
settype($r, "string");

echo $var1."\n";

echo $var2."\n";

echo $r."\n";

?>

Output:
0
3
1

You might also like