Download presentation
Presentation is loading. Please wait.
Published byPercival Lamb Modified over 9 years ago
1
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1 Wiley and the book authors, 2002 PHP Bible Chapter 6: Types in PHP
2
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition2 Wiley and the book authors, 2002 Summary Understanding the eight PHP types: integer, double, boolean, NULL, string, array, object, and resource Creating, reading, printing, and manipulating objects of different types Converting from one type to another
3
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition3 Wiley and the book authors, 2002 Don’t worry, be happy All programming languages have some kind of “type” system which specifies the different kinds of values that can appear in programs PHP makes it easy not to worry too much about typing of variables and values because it does not require variables to be typed and it handles a lot of type conversions for you PHP will generally do the “right” thing converting types when necessary $pi = 3 + 0.14159; results in a floating-point number (double) with the integer 3 implicitly converted into floating point before the addition is performed $sub = substr(12345, 2, 2); results in a string and the integer 12345 is converted to a string “12345” prior to executing the substr function which requires a string as the first argument
4
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition4 Wiley and the book authors, 2002 Type summary PHP has a total of eight types used implicitly in its code: Integers: whole numbers, without a decimal point (e.g. 495) Doubles: floating-point numbers (e.g. 3.14159 or 49.0) Booleans: only two possible values, TRUE and FALSE NULL: special type that only has one value, NULL Strings: sequences of characters (e.g. ‘PHP 4.0 supports string ops’) Arrays: named and indexed collections of other values Objects: instances of programmer-defined classes which can package up both other kinds of values and functions that are specific to that class Resources: special variables that hold references to resources external to PHP (e.g. database connections, file connections) Integers, doubles, booleans, NULL, and strings are simple types Arrays and objects are compound types (can package up arbitrary values of arbitrary types) Resources are special types that programmers do not manipulate directly other than requesting resources via special functions and passing them onto other functions that need them
5
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition5 Wiley and the book authors, 2002 Integers Simplest type, corresponding to whole numbers, both positive and negative Can be read in 3 formats: decimal (base 10) – default if no other base is specified octal (base 8) – specified with a leading 0 hexadecimal (base 16) – specified with a leading 0x PHP integers correspond to the C long type Max values depend on the word-size of your machine For most common platforms, the largest integer is 2 31 – 1 (2,147,483,647) Smallest negative integer is –(2 31 – 1) or -2,147,483,647 No MAXINT constant to check on the system you are running PHP on
6
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition6 Wiley and the book authors, 2002 Doubles Floating-point numbers (e.g. 123.456, 0.456, 2.0) By default, doubles print with the minimum number of decimal places needed Just because a numeric variable prints out as an integer does not necessarily mean that it is stored as an integer Doubles can be entered in normal or scientific notation $small_negative = -.12345; $small_positive = 5.5e-3;
7
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition7 Wiley and the book authors, 2002 Booleans Booleans are true-or-false values generally used in control statements like the “testing” portion of an if statement All other PHP types can be interpreted as booleans Numbers are false if exactly equal to zero, otherwise they are true Caveat: Don’t use doubles as booleans since they rarely equal exactly 0.0 (e.g. sqrt(2.0) * sqrt(2.0) – 2.0 does not equal 0) Strings are false if they are empty (no characters) or the string ‘0’, true otherwise NULL is always false Arrays and objects are false if they contain no other values Valid resources are true, false when the requested resource could not be assigned
8
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition8 Wiley and the book authors, 2002 NULL Only 1 possible value for the NULL type, NULL To assign the NULL value to a variable, simply assign it $my_var = NULL; NULL is capitalized by convention, but is case-insensitive NULL represents the lack of a value A variable that has been assigned the NULL value is nearly indistinguishable from a variable that has not been set at all It evaluates to FALSE in a boolean context Returns FALSE when tested with IsSet() PHP will not print warnings if you pass the variable to functions and back again, unlike variables that have never been set Use it when you want to make it clear to both the reader of your code and to PHP that this is what you want
9
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition9 Wiley and the book authors, 2002 Single-quoted Strings Strings are sequences of characters May be singly or doubly-quoted Single-quoted strings are read and store their characters literally $variable = ‘name’; $literally = ‘My $variable will not print!\n’; print($literally); would print “My $variable will not print!\n” To embed a single quote in a singly-quoted string, precede it with a \ To embed a backslash in a singly-quoted string, precede it with another \ Single-quoted strings execute much quicker than double-quoted strings since very little of the string has to be interpreted
10
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition10 Wiley and the book authors, 2002 Double-quoted Strings Strings delimited with double-quotes are preprocessed in both the following ways in PHP Certain character sequences beginning with a backslash are replaced with special characters \n is replaced with the newline character \r is replaced with the carriage return character \t is replaced with the tab character \$ is replaced with the dollar-sign character itself \” is replaced with a double-quote \\ is replaced with a backslash Variable names (anything starting with an un-escaped $) are replaced with string representations of their values Undefined variables are replaced with the empty string In both types of strings, newline characters can be inserted literally as well as with the \n escaped sequence in double-quoted strings making it easy to enter entire blocks of HTML code into a single string
11
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition11 Wiley and the book authors, 2002 Arrays Arrays give a programmer a means to group a bunch of different values together and index them by number or by name Can be used to replace variables like $thing1, $thing2, $thing3 with a single indexed variable array like $thing[1], $thing[2], $thing[3] Array elements are referred to via indices in brackets and elements of different types can be assigned into the same array To create an array element or change an existing elements value, simply assign a value to its index like a normal variable $thing[1] = “value”; $thing[2] = 3.111; print(“$thing[1] is $thing[2] - $thing”); Would output “value is 3.111 – Array”
12
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition12 Wiley and the book authors, 2002 Arrays (cont.) Arrays in PHP are implemented differently than arrays in other languages Instead of reserving contiguous blocks of predefined space, PHP arrays are allocated on the fly as space is needed Implemented as a hash rather than indexed memory locations PHP array elements can be created with arbitrary indices Array elements created without specifying the index begin with an index of 0 and are incremented automatically $things[] = 1; $things[] = 2; print(“$things[0]:$things[1]”); would output 1:2 Array indices can also be strings instead of integers and are known as associative arrays $food[‘Spanish’] = ‘paella’; $food[‘Scottish’] = ‘haggis’;
13
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition13 Wiley and the book authors, 2002 Objects PHP supports the use of objects in its attempt to utilize object- oriented programming techniques (OOP) Objects are similar to arrays in that it is a compound type which lets you package other data values together in a single unit Objects, however, have many more complex properties including the ability to package functions as wells as data and the capability to define new object types in terms of previously defined object types
14
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition14 Wiley and the book authors, 2002 Resources Resources are special values that refer to memory or system resources that are external to the PHP language itself Resources are not created by the programmers, they call special functions that return values of the resource type, and then pass these values to other functions that may require resources of that type $db_resource = mysql_connect(‘db_name’); mysql_query(‘SELECT * FROM table’,$db_resource);
15
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition15 Wiley and the book authors, 2002 Type Testing Because variables can change types due to reassignment or you cannot easily find where a variable’s type was set, PHP provides several functions to find out what the type of a value assigned to a variable is at execution time gettype(arg) : returns a string representing the type of the argument passed in ('integer', 'double', 'string', 'array', 'object', or 'unknown type') is_int(arg), is_integer(arg), is_long(arg) : returns TRUE if arg is an integer, false otherwise is_double, is_float, is_real, is_bool, is_null, is_string, is_array, is_object, is_resource all perform the same type checking as is_int for the appropriate type in the function name
16
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition16 Wiley and the book authors, 2002 Assignment and Coercion PHP generally automatically converts from one type to another when the content demands it. Generally, these automatic conversions result in the behavior the programmer wants Some conversions that don't make sense are undefined (e.g. Object type to numeric type) and the current behavior for your version of PHP may change with other versions of PHP Complete list of automatic conversions on page 95 Explicit conversions are also support in 3 ways Functions intval(arg), doubleval(arg), and strval(arg) will convert arg no matter what its source type to the target type in the name of the function Any expression can be type cast converting the result of the expression into the specified type by putting the name of the type in parentheses preceding the expression (e.g. (integer)$string_var * 5) settype(variable,target_type_string) works complimentary to the gettype(arg) function where you specify the type you want as a string in the second argument
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.