Chapter 3..PHP Basics (Part-2) |
||||
54 views
PHP’s Supported DatatypesA datatype is the generic name assigned to any data sharing a common set of characteristics. Common datatypes include Boolean, integer, float, string, and array. PHP has long offered a rich set of datatypes, and in this section you’ll learn about them. Scalar DatatypesScalar datatypes are capable of containing a single item of information. Several datatypes fall under this category, including Boolean, integer, float, and string. Boolean $alive = false; // $alive is false. $alive = 1; // $alive is true. $alive = -1; // $alive is true. $alive = 5; // $alive is true. $alive = 0; // $alive is false. Integer 42 // decimal -678900 // decimal 0755 // octal 0xC4E // hexadecimal The maximum supported integer size is platform-dependent, although this is typically positive or negative 231 for PHP version 5 and earlier. PHP 6 introduced a 64-bit integer value, meaning PHP will support integer values up to positive or negative 263 in size. 4.5678 4.0 8.7e4 1.23E+11 String $color = “maroon”; Compound DatatypesCompound datatypes allow for multiple items of the same type to be aggregated under a single representative entity. The array and the object fall into this category. $state[0] = "Alabama"; $state[1] = "Alaska"; $state[2] = "Arizona"; ... $state[49] = "Wyoming"; But what if the project required correlating U.S. states to their capitals? Rather than base the keys on a numerical index, you might instead use an associative index, $state["Alabama"] = "Montgomery"; $state["Alaska"] = "Juneau"; $state["Arizona"] = "Phoenix"; ... $state["Wyoming"] = "Cheyenne"; Arrays are formally introduced in Chapter 5, so don’t worry too much about the matter if you don’t completely understand these concepts right now. Object class Appliance {
private $_power; function setPower($status) {
$this->_power = $status; } } ... $blender = new Appliance; A class definition creates several attributes and functions pertinent to a data structure, in this case a data structure named Appliance. There is only one attribute, power, which can be modified by using the method setPower(). Remember, however, that a class definition is a template and cannot itself be manipulated. Instead, objects are created based on this template. This is accomplished via the new keyword. Therefore, in the last line of the previous listing, an object of class Appliance named blender is created. $blender->setPower("on");
Improvements to PHP’s object-oriented development model are a highlight of PHP 5 and are further enhanced in PHP 6. Chapters 6 and 7 are devoted to thorough coverage of PHP’s object-oriented development model. Converting Between Datatypes Using Type CastingConverting values from one datatype to another is known as type casting. A variable can be evaluated once as a different type by casting it to another. This is accomplished by placing the intended type in front of the variable to be cast. A type can be cast by inserting one of the operators shown in Table 3-2 in front of the variable. Table 3-2. Type Casting Operators ————————————————————————— ————————————————————————— ————————————————————————— Let’s consider several examples. Suppose you’d like to cast an integer as a double: $score = (double) 13; // $score = 13.0 Type casting a double to an integer will result in the integer value being rounded down, regardless of the decimal value. Here’s an example: $score = (int) 14.8; // $score = 14 What happens if you cast a string datatype to that of an integer? Let’s find out: $sentence = "This is a sentence"; echo (int) $sentence; // returns 0 In light of PHP’s loosely typed design, it will simply return the integer value unmodified. $score = 1114; $scoreboard = (array) $score; echo $scoreboard[0]; // Outputs 1114 Note that this shouldn’t be considered standard practice for adding items to an array $model = "Toyota"; $obj = (object) $model; The value can then be referenced as follows: print $ obj->scalar; // returns "Toyota" Adapting Datatypes with Type JugglingBecause of PHP’s lax attitude toward type definitions, variables are sometimes automatically cast to best fit the circumstances in which they are referenced. Consider the following snippet: <?php $total = 5; // an integer $count = "15"; // a string $total += $count; // $total = 20 (an integer) ?> The outcome is the expected one; $total is assigned 20, converting the $count variable from a string to an integer in the process. Here’s another example demonstrating PHP’s type-juggling capabilities: <?php $total = "45 fire engines"; $incoming = 10; $total = $incoming + $total; // $total = 55 ?> The integer value at the beginning of the original $total string is used in the calculation. However, if it begins with anything other than a numerical representation, the value is 0. Consider another example: <?php $total = "1.0"; if ($total) echo "We're in positive territory!"; ?> In this example, a string is converted to Boolean type in order to evaluate the if statement. <?php $val1 = "1.2e3"; // 1,200 $val2 = 2; echo $val1 * $val2; // outputs 2400 ?> Type-Related FunctionsA few functions are available for both verifying and converting datatypes; they are covered in this section. Type Identifier FunctionsA number of functions are available for determining a variable’s type, including is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), <?php $item = 43; printf("The variable \$item is of type array: %d <br />", is_array($item));
printf("The variable \$item is of type integer: %d <br />",
is_integer($item)); printf("The variable \$item is numeric: %d <br />", is_numeric($item));
?> This code returns the following: IdentifiersIdentifier is a general term applied to variables, functions, and various other userdefined objects. There are several properties that PHP identifiers must abide by: Table 3-3. Valid and Invalid Identifiers ——————————————————— Valid Invalid ——————————————————— ——————————————————— • Identifiers are case sensitive. Therefore, a variable named $recipe is different from a variable named $Recipe, $rEciPe, or $recipE. |
| « C# Day 2 - Objects and Types (part 1) | C# Day 2 - Objects and Types (part 2) » |
| Posted on Monday, February 9th, 2009 at 3:40 pm under PHP and MySQL - From Beginning to Professional | RSS 2.0 Feed | |
February 9th, 2009 at 4:41 pm
[...] Chapter 3..PHP Basics(Part-2) (PHP’s Supported Datatypes,Scalar Datatypes,Compound Datatypes,Converting Between Datatypes Using Type Casting,Adapting Datatypes with Type Juggling,Type-Related Functions,Type Identifier Functions,Identifiers) [...]