| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Data TypesThese tutorials are about PHP and its use for server-side Web programming. PHP Data Typesdata type: a general term to describe categories of values that a program or programming language can make use of A data type refers to the type of data a variable can store. PHP has eight (8) different data types you can work with. These are:
PHP is an loosely-typed language, so a variable does not need to be of a specific type and can freely move between types as demanded by the code it is being used in. But you still need to know your data types in order to be able to work with the language.
First the special cases, resources and Resources are not an actual data type, but the storing of a reference to functions and resources external to PHP. The most common example of using the resource data type is a database call. We will not talk about them here, since they are an advanced topic.
Null is a special data type which can have only one value, which is itself. Which is to say, So why is there garbage in memory in the first place? The reason is simple. When computers assign a variable a value, what they do is assign the variable a location in memory and then store the value in that location. The variable is really a reference to that location in memory, containing the memory address and what type of information the computer should expect to find there. It does not actually store the value, just a reference to its location. When the computer is done with the variable or is told to delete it, it does not delete the value in memory, but the reference to its location. This is far more efficient that deleting the actual values, but it means that whatever garbage was left in that location in memory is still there. This is also why you have to wrestle with pointers in programming courses they are how computers really work with variables and values. Of the other two data types, we can lump everything into two categories. The first category is scalar, or primitive, data types. They are data types that can only hold a single value. These are integers, floating point numbers, strings, and booleans. The second category is the compound data types, data types that store a collection of values. These are objects and arrays. Integersinteger: a whole number, a number with no fractional component
An integer is a whole number. That is to say, it is a number with no fractional component. For those who are familiar with C, a PHP integer is the same as the decimal: a base ten numbering system Integers can be written in decimal, octal, or hexadecimal. Decimal numbers are a string of digits with no leading zeros. Any integer may begin with a plus sign ( + ) or a minus sign ( - ) to indicate whether it is a positive or negative number. If there is no sign, then positive is assumed. Valid decimal integers: 1 123 +7 -1007395 octal: a base eight numbering system An octal number is a base-8 number. Each digit can have a value between zero (0) and seven (7). Octal numbers are commonly used in computing because a three digit binary number can be represented as a one digit octal number. Octal numbers are preceded by a leading zero (e.g., 0123). Valid octal integers: 01 0123 +07 -01007345 hexadecimal: a base sixteen numbering system A hexadecimal number is a base-16 number. Each digit can have a value between zero (0) and F. Since we only have ten numbers in our numbering system (0-9), we use the letters A through F to make up the difference for hexadecimal values. Hexadecimal values are common in computing because each digit represents 4 binary numbers, which is four bits. Eight bits, or a two digit hexadecimal numer, is one byte. Hexadecimal numbers are preceded by a leading zero and X (e.g., 0x123). Valid hexadecimal integers: 0x1 0xff 0x1a3 +0x7 -0x1ab7345 So what about zero, you may ask. If anything beginning with a zero is octal then isn't zero octal? And how do we represent a decimal zero then? The answer is simple. It makes no difference. If you have zero of something, it doesn't matter how you count it, it is still zero.
If you want to find out whether a variable stores an integer you can use the Floating Point Numbersfloating-point number: numbers with a fractional component
Floating-point numbers are also sometimes called real numbers. They are numbers that have a fractional component. Unlike basic math, all fractions are represented as decimal numbers. If you are familiar with C, PHP floating-point numbers are equivalent to the
PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point. The second is a floating-point number written in scientific notation. Scientific notation takes the form of Some examples of valid floating point numbers include: 3.14 0.001 -1.234 0.314E2 // 31.4 1.234E-5 // 0.00001234 -3.45E-3 // -0.00345
If you want to know if a variable contains a floating-point number, you can use the Stringsstring: a text literal of an arbitrary length A string is a text literal of an arbitrary length. Most of working with Web pages is about working with strings. A string is indicated by being enclosed in quotes, either double or single quotes. Unlike some programming languages, PHP differentiates between single and double quotes. Strings inside double quotes are parsed, or interpolated, while strings inside single quotes aren't. What this means is that if you include variables or special characters in double-quoted strings, those values are processed and become part of the string. Putting variable names and special characters in single-quoted strings causes the variable names and special character escape sequences to be written out exactly as you typed them. In other words, they are literals. This means that you can embed variables directly inside strings in PHP when using double quotes, but not when using single quotes. This make concatenating strings a little easier. Thus we have the following situation: $myVar = "xyz"; // assign a value to $myVar echo $myVar; // writes 'xyz' echo "abc to $myVar"; // writes 'abc to xyz' // but with single quotes echo 'abc to $myVar': // writes 'abc to $myVar' This does make it easy to write PHP code out inside a string, as well as to process PHP code within a string. Special Characters
Anything within quotes is part of a string, even numbers. Thus,
In PHP, you escape a character by preceding it with a backslash. There are two types of escaped characters in PHP. There are those that are a special character in the language and need to be escaped to get the parser to treat them as a literal. For instance,
Just so you know:
Special characters can either be a single character preceded by a backslash or a numeric value in either octal or hexadecimal preceded by a backslash. Some of the more common escape characters are:
Strings in single quotes are not technically parsed, but you can use
One important thing you shouldn't have in a string is the sub-string
You can use the PHP includes many string related functions, since much of what it does is string processing. These are discussed elsewhere. Here Documents
The heredoc string structure is a method of including larger strings inside your code. You can use it to include content of any length. To create a heredoc, you use a special operator that is made up of three left brackets ( $longString = <<< termination_marker any amount of content termination_marker; For instance: $longFellowNot = <<< End_of_verse This short poem May not fill a tome, But it serves to show How it is that heredoc goes. End_of_verse; Anything between the termination markers is preserved, included all white space, quote marks, and special characters. The last carriage return before the closing termination marker is omitted, so if you want one, you should leave a blank line after the end of the content and before the termination marker. You just need to make sure that your termination string does not occur anywhere in the content being delimited. Booleans
A boolean value assesses the truth value of something. Booleans only have two values, If a conditional is converted to a different data type, then true equates to one (1) and false equates to zero (0). The conversion in the other directions is a little more complex. If testing for the truth value of a non-boolean value, any of the following values will equate to false.
All other values are true, including all resource values. Compound Data TypesArraysAn array is a variable that holds a group of values. Arrays are usually meant to store a collection of related data elements, although this is not a necessity. You access the individual elements by referring to their index position within the array. The position is either specificied numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them. Indexed arrays start at position zero, not at position one, so your first array element has an index of 0, and the highest position in the array is one less than the number of elements in the array. Referencing array elements is done with the following notation: $arrayName[index]; You assign a value to an array position by specifying which array element you want to assign a value to: $listing[0] = "first item"; $listing[1] = "second item"; $listing[2] = "third item"; An individual array element can be of any type, including another array.
If you want to find out if a variable contains an array you can use the
We deal extensively with arrays in their own section. If you haven't gotten to it yet, beware of the fact that there are some counterintuitive elements in how PHP deals with arrays. For example, given the following statements, what is the value of $arName["item1"] = "abc"; $arName["item2"] = "def"; $arName["item3"] = "ghi";
For those who said that I can't fool them because the answer is "def" not "abc", then I just fooled you. The answer is that ObjectsPHP is capable of functioning as an object-oriented programming language (or OOP). As such, it must be able to handle objects. An object is a data type that allows for the storage of not only data but also information on how to process that data. The data elements stored within an object are referred to as its properties, also sometimes called the attributes of the object. The information, or code, describing how to process the data compromises what are called the methods of the object. Objects have two components to their construction. First, you must declare a class of object. It defines the structure of the object to be constructed. Then you instantiate the object, which means you declare a variable to be of a certain class and assign values to it appropriately. Another way of looking at objects is that they allow you to create your own data types. You define the data type in the object class, and then you use the data type in instances of that class.
Yes, there is also an Objects have their own section in these notes.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|