- A data type refers to the type of data that a variable can store.
- PHP offers eight various data types to work with. They are as follow:
- Integer number
- Float-point number
- In simple word, an integer is a whole number without fractional part.
- A PHP integer is the same as the long data type in C.
- It’s rang is from -2,147,483,648 to +2,147,483,647.
- Integers can be represented in decimal, octal, or hexadecimal number system.
- Decimal numbers are a string of digits with no leading zeros.
- Integer may written with a plus ( + ) or a minus sign ( – ) to represent positive or negative number. If there is no sign, then positive is assumed.
- Example of some valid decimal integers: 1, 125, +32, -270
- An octal number is a base-8 number.
- In octal number system, each digit can have a value between zero (0) and seven (7). Octal numbers are preceded by a leading zero (e.g., 0123).
- They are normally used in computing because a three digit binary number can be represented as a one digit octal number.
- Example of some valid octal integers: 01, 0123, +07
- A hexadecimal number is a base-16 number.
- In hexadecimal number system, each digit can have a value between zero (0) to nine (9) and letters A to F (to represent 11 to 16).
- Hexadecimal numbers are preceded by a leading zero and X (e.g., 0x123)
- Hexadecimal values are common in computing because each digit represents 4 binary numbers, which is four bits.
- Example of some valid hexadecimal integers: 0x1, 0xff, 0x1a3, +0x7
- If anything beginning with a zero is octal then you might wonder what about zero? And how do we represent a decimal zero then?
- Well the answer is simple. It makes no difference. If you have zero of something, it doesn’t matter how you count it as it is still zero.
- You can use is_integer() function ( is_int() for short) to check if a variable holds an integer or not. If it is, then the function returns true.
Floating Point Numbers
- Floating-point numbers, also called real numbers are numbers with a fractional part. PHP floating-point numbers are equivalent to the double data type in C.
- 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 [number] E [exponent], for example, 1.75E-2.
- Some examples of valid floating point numbers are: 3.14, -1.234, 0.314E2, -3.45E-3
- We can use is_float() function to check if variable holds floating point value or not. the function will return true if it is a floating-point number.
- A string is a text literal of arbitrary length. A string is represented by using either double or single quotes.
- Unlike some programming languages, PHP treats single and double quotes differently.
- Strings inside double quotes are parsed, while strings inside single quotes are not. 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 = "abc" ; // assign a value to $myVar echo $myVar ; // writes 'abc' echo "hello to $myVar"; // writes 'hello to abc' echo 'hello to $myVar' ; // writes 'hello 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.
- A Boolean variable can store only “true” or “false”.
- All conditional expression evaluates to either true or false boolean value based on the condition being tested.
Compound Data Types
- An array is a variable that holds a group of values a collection of related data elements.
- Individual array elements can be accessed by referring to their index position within the array. The position is either specified numerically or by name.
- An array with a numeric index is commonly called an indexed array while an array having named positions is called an associative array. In PHP, all arrays are associative, but we can still use a numeric index to access them.
- Indexed arrays start from position zero. and the highest position in the array is n-1 where, n is the number of elements in the array.
- Array elements are referred with the following syntax:
- We can assign a value to an array position by specifying its element index as follow:
$items = "Item One" ; $items = "Item Two" ; $items = “Item Three" ;
- An individual array element can be of any type, including another array.
- We can use is_array() function to find out if a variable contains an array.
- An object is a data type that allows for the storage of data as well as information on how to process that data.
- PHP is an object-oriented programming language so it is able to handle objects. The data elements stored within an object are referred to as its properties, also called the attributes of the object.
- The information, or code, describing how to process the data, is called methods of the object.
- In PHP, an object must be explicitly declared from the class.
- A class is a container or logical structure that contains both properties and methods. “class” keyword is used to declare it.
- Resources are not an actual data type, but it is used for storing reference to functions and resources external to PHP.
- The most common example of using the resource data type is a database call.
- Null is a special data type which has no value assigned to it.
- If a variable is created without a value, it is automatically assigned a value of null.