A data type identifies the characteristics that the assigned data will be interpreted by when interacting with
the PHP programming language. When a value is assigned a specific data type, the PHP interpreter will
work with the data based on the expected type of data it is. For example, 27654 could be considered a
numeric data type or it could be considered a string data type, such as in the zip code for a customer. Even
though you could perform mathematical equations on the zip code, it would make no sense to do so. Thus,
zip codes, even though they look like they would be numeric numbers should be identified as a string data
type to eliminate problems such as zip codes that start with zero (ex. 08102). Assigning the correct data
type to the expected value is an important part of working with PHP. There are three categories of data
types in PHP: Scalar, Compound and Special.
Scalar Data Types
A scalar data type is identified as being able to hold
one value at a time. In PHP, there are four data
types that fall into this category.
• Boolean - A boolean value is a truth value, either 'true' or 'false', often coded 1 and 0, respectively.
When a value is assigned the Boolean data type, there are only two responses that the value can be
assigned: True (1) or False (0). If a value is assigned anything other than these two values, it will
default to True (1).
Try the following program:
If you tried the above program, you would have noticed that no value was displayed for false; do not worry about that.Boolean values are usually the result of operators in conditionals like the if-condition. Read and try the following program:
The following returned values are equivalent to FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string (""), and the string "0"
- an array with zero elements
- the special type NULL (including unset variables) – See later
- SimpleXML objects created from empty tags – See later
The following returned values are equivalent to TRUE:
- Any literal or data type that is not in the above list is equivalent to TRUE.
--------------------------------------------------------------------------------------------------------------
• Integer - An integer is a whole numeric data type (meaning it does not contain any fractions) that
can be set to decimal (base 10), octal (base 8) or hexadecimal (base 16). The following
demonstrate some of the values that can be assigned the numeric data type:
The maximum number that can be assigned to an integer is based on the system. 32 bit systems
have a maximum signed integer range of -2147483648 to 2147483647. The maximum signed
integer value for 64 bit systems is 9223372036854775807. Any value that exceeds the maximum
integer size will be assigned the float data type.
• Float - Floating point numbers are any number that has a fractional component or exceed the
integer maximum values. This data type is also referred to as floats, doubles or real numbers.
Floats are used to represent such numbers as monetary values, distances, weights, scientific
notations and a host of other values:
The size of a floating point number is dependent by the system being used. In addition, floating
point numbers are not as accurate as integer numbers when it comes to precision. Even simple
numbers like 0.2 and 0.17 can not be converted into their binary equivalents without a loss of
precision. With that said, floating point numbers should never be used in comparison operations
without building in logic such as rounding numbers prior to the comparison operation or using a
tolerance value that would be an acceptable difference between the compared values for
comparison operations.
• String - A string data type is a series of characters that are associated with each other in a defined
order. There is no limit to the length of a string data value. The following represent some of the
values that can be assigned to a string data type:
The characters that can be used in PHP string data types are limited to the ISO 8859-1 or Latin-1
character set. This character set allows for 256 (the size of a byte) different possible characters
(191 of them that are actually printable); however, there is support in PHP to encode strings to the
UTF-8 standard (UTF-8 is a standard mechanism used by Unicode for encoding wide character
values into a byte stream).
the PHP programming language. When a value is assigned a specific data type, the PHP interpreter will
work with the data based on the expected type of data it is. For example, 27654 could be considered a
numeric data type or it could be considered a string data type, such as in the zip code for a customer. Even
though you could perform mathematical equations on the zip code, it would make no sense to do so. Thus,
zip codes, even though they look like they would be numeric numbers should be identified as a string data
type to eliminate problems such as zip codes that start with zero (ex. 08102). Assigning the correct data
type to the expected value is an important part of working with PHP. There are three categories of data
types in PHP: Scalar, Compound and Special.
Scalar Data Types
A scalar data type is identified as being able to hold
one value at a time. In PHP, there are four data
types that fall into this category.
• Boolean - A boolean value is a truth value, either 'true' or 'false', often coded 1 and 0, respectively.
When a value is assigned the Boolean data type, there are only two responses that the value can be
assigned: True (1) or False (0). If a value is assigned anything other than these two values, it will
default to True (1).
<?php
$Wealthy = false; // $Wealthy is false
$Wealthy = 1; // $Wealthy is true
$Wealthy = 5; // $Wealthy is true
$Wealthy = -1; // $Wealthy is true
$Wealthy = 0; // $Wealthy is false
?>
A Boolean Value is either TRUE or FALSE typed in either upper or lower case and should not be in quotes.Try the following program:
<?php
$var0 = true;
$var1 = false;
$var2 = TRUE;
$var3 = FALSE;
echo $var0."<br />";
echo $var1."<br />";
echo $var2."<br />";
echo $var3."<br />";
?>
If you tried the above program, you would have noticed that no value was displayed for false; do not worry about that.Boolean values are usually the result of operators in conditionals like the if-condition. Read and try the following program:
<?php
if (true)
{
echo "It is true indeed."."<br />";
}
if (5==5)
{
echo "5==5 as a condition with the operator, ==, returns TRUE."."<br />";
}
?>
TRUE or FALSE are Boolean literals. There are other literals that are
equivalent to TRUE or FALSE. That is what we look at in this section. The following returned values are equivalent to FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string (""), and the string "0"
- an array with zero elements
- the special type NULL (including unset variables) – See later
- SimpleXML objects created from empty tags – See later
The following returned values are equivalent to TRUE:
- Any literal or data type that is not in the above list is equivalent to TRUE.
--------------------------------------------------------------------------------------------------------------
• Integer - An integer is a whole numeric data type (meaning it does not contain any fractions) that
can be set to decimal (base 10), octal (base 8) or hexadecimal (base 16). The following
demonstrate some of the values that can be assigned the numeric data type:
<?php
$Temperature = 22; // $Temperature is an integer
$x = -45672; // $x is an integer number
$y = 0642; // $y is an integer number (octal)
$z = 0x5E5B; // $z is an integer number (hexadecimal)
?>
The maximum number that can be assigned to an integer is based on the system. 32 bit systems
have a maximum signed integer range of -2147483648 to 2147483647. The maximum signed
integer value for 64 bit systems is 9223372036854775807. Any value that exceeds the maximum
integer size will be assigned the float data type.
• Float - Floating point numbers are any number that has a fractional component or exceed the
integer maximum values. This data type is also referred to as floats, doubles or real numbers.
Floats are used to represent such numbers as monetary values, distances, weights, scientific
notations and a host of other values:
<?php
$TotalCost = 67.25; // $TotalCost is a float
$Distance = 4.5e5; // $Distance is a float equal to 450000
$Weight = 92.0; // $Weight is a float
$Diff = 2.34E+12; // $Diff is a float (2340000000000)
?>
The size of a floating point number is dependent by the system being used. In addition, floating
point numbers are not as accurate as integer numbers when it comes to precision. Even simple
numbers like 0.2 and 0.17 can not be converted into their binary equivalents without a loss of
precision. With that said, floating point numbers should never be used in comparison operations
without building in logic such as rounding numbers prior to the comparison operation or using a
tolerance value that would be an acceptable difference between the compared values for
comparison operations.
• String - A string data type is a series of characters that are associated with each other in a defined
order. There is no limit to the length of a string data value. The following represent some of the
values that can be assigned to a string data type:
<?php
$Name = 'Jose Vargás'; // $Name is a string
$Value = '£45.20'; // $Value is a string
$Location = "subway\n"; // Contains a character return
$x = "This is a 'test'"; // $x is a string
$Chapter = '1.2.4' // $Chapter is a string
?>
The characters that can be used in PHP string data types are limited to the ISO 8859-1 or Latin-1
character set. This character set allows for 256 (the size of a byte) different possible characters
(191 of them that are actually printable); however, there is support in PHP to encode strings to the
UTF-8 standard (UTF-8 is a standard mechanism used by Unicode for encoding wide character
values into a byte stream).
No comments:
Post a Comment