Programming in PHPBy Peter McNulty : 20-11-2004
4.28 (118)
Types
A type is a name for a certain type of data. When a variable has a specific type, this determines the range of values that can be stored as well as the operations that can be performed on it. Some may consider this unfortunate, but PHP is dynamically typed. This means that you don't actually have to define the type of variable you are using. Some people consider this a bad point about PHP as it could under some circumstances be insecure, but mainly because it can be confusing if you are managing many variables.
There are several types of types (no pun intended) that PHP has, of which most are common to other languages. These are: Usage of Types <?php These are how the main types work. There are others, however, they are for more complicated issues and will be explained later. From the above, you can see that Boolean variables represent true or false. This can be useful in checking if something has been done already or if a condition is met. An integer type simply represents an integer. This is a whole number, therefore no decimal part. This is commonly used for the index in loops and counting functions. Float types represent a larger range of numbers, which include both integers and fractions. This can be used to represent more accurate data for calculations. Finally there are the string and null types. We have seen strings in previous examples as they are simply text. You can represent any characters, including digits using a string. The last type is the null type. Technically, a variable can only have a null type if there is nothing in it. This is a unique type to PHP, because as PHP has dynamic typing, when it is first initialized (if it is), then PHP may not know the type so it says it is empty. By setting a variable to null type, we thereby remove all data that was stored in it. Sometimes you may want to make sure a variable is of a certain type. This is commonly used in parsing user input to make sure they have entered valid data, or just when you want to convert types. There are two ways to do this: casting or using the settype() function. We will concentrate on the casting method. Casting to Boolean We can cast a variable to be boolean by preceding the value with (bool). This will evaluate to FALSE if the previous variable was equal to 0, 0.0, NULL, or and empty string. Otherwise it will evaluate to true. In this example, $x = TRUE, $y = FALSE and $z = TRUE. Casting to Boolean <?php Casting to Integer Casting to an integer may be useful in finding the integer value of fractions, or doing calculations with Booleans. As before, we precede the value with (int) to cast to an integer. When converting floats to integers, the decimal is simply cut off, so it is likely rounding down to the nearest integer. When casting a NULL type to an integer, you get back a NULL type, so this has no effect. When casting a string to an integer, strange things happen. If you actually want to do this, then I recommend you check out this string conversion page. When casting a Boolean to an integer, the value will be 1 if the Boolean was TRUE, or 0 otherwise. Casting to Integer <?php If we ran this script then $w = 2, $x = NULL, $y = 10 and $z = 1. Casting to Floating Point Casting to a float is similar to casting to an integer. To cast as a float we use (float) before the value / variable. As a float can represent both integers and fractions, then there is no change between converting from an integer to a float. The same process happens with Boolean variables and integer casting. If the variable equals true, then the float variable will be 1, or if false, then 0. This will not be demonstrated as it is so similar to integer casting. Casting to Strings Casting to strings is very simple in that, the string value will be exactly as we would write the value on paper. This of course assumes we write Boolean values as 0 and 1 as they are represented in binary instead of true and false. Hence we use the (string) statement: Casting to String <?php The values would be $x = '2', $y = '2.3' and $z = '0'. These are the main types of casting that you may want to do, however, as noted before, PHP is dynamically typed. Therefore, PHP will convert from one type to another for you, and will also accurately guess what type you want a variable to be. So casting is only really necessary when validating input. Even when you use the echo() function to output some text to the screen, PHP knows to convert all your variables into strings. Build Your Own Database Driven Website Using PHP & MySQL |
Ads
|