php Data Types

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 flexiblyin 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 you have created code that is designed 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 Double 3.234 A floating-point number
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)
NULL An uninitialized variable

Resource types are often returned by functions that deal with external applications or files. For example, you will see references to “the MySQL resource ID” in Chapter 18, “Interacting with MySQL Using Built-In PHP Functions.” The NULL type is reserved for variables that have been declared, but no value has been assigned to them.

PHP has several functions available to test the validity of a particular type of variableone 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.

By the Way

You can read more about calling functions in Chapter 7.

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 we declare the $testing variable in line 2, we do not assign a value to it, so when we test the variable in line 3 to see if it is null (using is_null()), the result is 1 (true). After this, we assign values 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 $testingonly 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 that we focus on in this chapter.

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:  ?>

By the Way

Per the PHP Manual, “double” is returned in case of a float, and not simply “float”. Your eyes are not deceiving you.

In each case, we use the appropriate is_* function to confirm the new data type and also 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: ?>

We never actually change the type of the $undecided variable, which remains a double throughout this script, as illustrated on line 15, where we use the gettype() function to determine the type of $undecided.

By the Way

Despite its usage here, don’t use gettype() to test for a certain type because it can be slow and is likely to be deprecated in future versions. Use the is_* family of functions to test type in production. This usage is simply for illustrative purposes.

In fact, by casting $undecided, we create 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 also in lines 5, 7, 9, and 11. Because we are only working with a copy of $undecided and not the original variable, it never lost its original value, as the $undecided variable did in line 6 of 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 will automatically cast 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 typethus, 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"

will provide an answer of 70.

By the Way

The generic term “number” is used here to mean integers and floats. If the user input is in float form, and the strings added together were “3.14cm” and “4.12cm”, the answer provided would be 7.26.

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, while “30cm” is transformed into “30”, the string “6ft2in” becomes just 6 because the rest of the string evaluatesto 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 their numberinstead 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 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).

Why Test Type?

Why might it be useful to know the type of a variable? There are often circumstances in programming in which data is passed to you from another source. In Chapter 7, you will learn how to create functions in your scripts, and data is often passed between one or more functions, as they can accept information from calling code in the form of arguments. For the function to work with the data it is given, it is a good idea to first verify that the function has been given values of the correct data type. For example, a function that is expecting data that has a type of “resource” will not work well when passed a string.

آموزش طراحی اختصاصی سایت با خرید بسته آموزش طراحی سایت PHP از

ارایه درگاه پرداخت اینترنتی برای سایتهای برنامه نویسی شده با PHP

در صورتیکه میخواهید تبلیغات خود را در اینترنت انجام دهید میتوانید در درج آگهی کنید