PHP Data Types PHP

PHP has three elemental types of data: integers, floating-point numbers, and strings of text. Integers are sometimes referred to as whole or natural numbers; they contain no decimal point. Floating-point numbers are sometimes called real numbers. They always contain a decimal point, even when only a zero follows it. PHP refers to these as doubles, which is short for double-precision floating-point numbers. Strings are collections of textual ata. String constants are always surrounded by double quotes (") or single quotes (').

In addition to these, PHP has four aggregate data types that use the other three: arrays, objects and booleans, and resources. An array is a collection of values associated with indexes. Objects are similar to arrays, but may also contain functions. They are discussed in Chapter 6, "Classes and Objects." Boolean values are either true or false. Historically, PHP did not support a separate type for booleans; instead zero and an empty string were understood to be false, while any other value was considered to be a true value. With PHP 4, this changed. Now data may be cast or set to be of boolean type. Resources are integers used to identify system resources, such as open files or database connections.

As you write PHP code, you will usually be unaware of the distinction between types because variables are multitype. You do not declare a variable to be a particular type. You just assign it a value. PHP will remember what type of data you put into the variable. When you retrieve data from the variable, they are returned with that same type.

There are two ways to override this behavior. The first way is to use the settype function. This tells PHP that you want to start considering a variable to be a certain type. The data associated with the variable will be converted to the new type. The alternative is to use one of the type conversion functions or a cast. Consider Listing which contrasts settype, the type conversion functions, and casts.
Experimenting with Type Converstion

Experimenting with Type Converstion

Experimenting with Type Converstion

When AverageTemperature is first used, PHP marks it internally as a string because it is assigned the value of a string literal. Setting the type to be double causes the value to be reevaluated. If you check the output, youwill notice that some information is lost as a result. The text following the number is dropped off because it has no meaning in the context of a floating-point number. Likewise, when the script sets the type to be integer, the fractional part of the number is dropped. Even when we change the type back to string, the previous information is gone.

In contrast to this, the use of the type conversion commands preserves the value of the variable because it does the conversion on the fly. The data inside the variable are not changed. . Casts, identical in operation to type conversion functions, take the form of preceding an expression with a datatype in arentheses. Valid casts are (boolean), (integer), (string), (double), (array) , and (object) .

Another type of data sometimes discussed in this text is the bitfield. Rather than a data type exactly, it is a way of viewing an integer. Instead of a single value, it is viewed as a sequence of ones and zeroes. Bitfields are discussed later in this chapter in relation to bitwise operators

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics