Two Data Type Categories
- Scalar - contains only one value at a time
- Composite or Compound - array and objects
Four scalar types supported
- boolean - a value that can only either be true or false
- int - a signed numeric integer value
- float - a signed floating-point value
- string - a collection of binary data
Numeric Values
PHP recognizes two types of numbers
- integers
- the int data type is used to represent signed integers - floating-point values
- also called float and sometimes double, are numbers that have a fractional component
Numeric Notations
- Decimal - standard decimal notation.
ex.10; -11; 1452
Note: no thousand separator is needed or allowed - Octal - identified by its leading zero (like the access permission of Unix)
ex.0666, 0100
- Hexadecimal
ex.0x123, 0XFF; -0x100
Note
The leading 0x prefix are both case-insensitive
Note
Octal numbers can easily confused with decimal numbers and can lead to some consequences
Two supported notations for Floating-point numbers
- Decimal - traditional decimal notation
ex.0.12; 1234.43; -.123
- Exponential - composed of a mantissa value followed by a case-insensitive letter E then an exponent value.
ex.2E7, 1.2e2
Note
- The precision and range of both types varies depending on the platform on which your scripts run
ex. 64-bit platforms may be capable of representing a wider range of integer numbers than 32-bit platforms - PHP doesn't tract overflows
- Float data type is not always capable of representing numbers in the way you expect it to.
ex.echo (int)((0.1 + 0.7) * 10);
this results to 7 instead of 8 since the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8.0.
Strings
An ordered collection of binary data.
- could be text, content of an image file, spreadsheed or even a music recording.
Booleans
- can only contain either TRUE or FALSE.
- used as basis for logical operations.
Note
When converting data to and from the Boolean type, several rules apply:
- When a numeric variable is converted to boolean data type, the value becomes false if the variable contains zero and true otherwise.
- When a string variable is converted to boolean data type, the value becomes false only when the variable is empty or if it contains a single character 0. Other values, it is converted to true.
- When a boolean variable is converted to a numeric or string, it becomes 1 if it is true, and 0 otherwise.
No comments:
Post a Comment