Showing posts with label PHP Data Types. Show all posts
Showing posts with label PHP Data Types. Show all posts

Friday, August 28, 2009

PHP Data Types


Bookmark and Share


Two Data Type Categories


  1. Scalar - contains only one value at a time

  2. Composite or Compound - array and objects

Four scalar types supported


  1. boolean - a value that can only either be true or false

  2. int - a signed numeric integer value

  3. float - a signed floating-point value

  4. string - a collection of binary data

Numeric Values


PHP recognizes two types of numbers


  1. integers
    - the int data type is used to represent signed integers

  2. floating-point values
    - also called float and sometimes double, are numbers that have a fractional component

Numeric Notations


  1. Decimal - standard decimal notation.
    ex. 10; -11; 1452
    Note: no thousand separator is needed or allowed

  2. Octal - identified by its leading zero (like the access permission of Unix)
    ex. 0666, 0100

  3. 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


  1. Decimal - traditional decimal notation
    ex. 0.12; 1234.43; -.123

  2. Exponential - composed of a mantissa value followed by a case-insensitive letter E then an exponent value.
    ex. 2E7, 1.2e2

Note
  1. 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

  2. PHP doesn't tract overflows

  3. 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:
  1. When a numeric variable is converted to boolean data type, the value becomes false if the variable contains zero and true otherwise.

  2. 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.

  3. When a boolean variable is converted to a numeric or string, it becomes 1 if it is true, and 0 otherwise.


Bookmark and Share